I want to execute a complex command-line command when I click a cell in excel, a hyperlink cell. I've tried everything but I can't get it to work.
You can right-click and choose Hyperlink but the dialog doesn't support complex arguments (arguments with double quotes). You simply get an error "Invalid link".
You can use the =HYPERLINK() formula but that doesn't support arguments at all. Only web URLs are executed, such as mailto: and callto:. URLs can have parameters passed with &arg=value but that's no good for command-line arguments.
I don't know what else to try, any ideas?
You need to pass a Shell command with the following syntax
e.g.
Sub RunCalculator()
Dim ret as Double 'Optional
ret = Shell("C:\Windows\System32\Calc.exe")
MsgBox(ret) ' in case you are interested in the return value
End Sub
To execute when clicking a cell:
You need to put your code in a macro that's invoked from the Worksheet_SelectionChange() event which you can get by using the dropdown in the VBE when you open the code for the worksheet in the editor.
Unrelated side note: The name reminds of the times when one used the DOS shell
Related
I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".
My excel sheet creates three proper sentences at three different locations in the worksheet based on the data in the tables but has double spaces between some of the words. I am trying to find a code where I can click a button that copies information from these different arrays in the worksheet that I can then paste into a word file without any double spaces or tab spaces and add some additional generic lines.
Currently, we are manually copying information from these arrays into a notepad to remove the table, the cells and their formating. Then manually replace all the double space and tab spaces with a single space, add some additional generic lines and then copying this information into a Word file.
Trying to write a code that will allow me to do this by clicking the button and then pasting it anywhere I want. What code can I use for this? Thanks.
Here are some ideas, untested so you will probably have to experiment to get them to work. If you get stuck, please use google to research the problem, then post your code and issue in a new question, so folk have more to go on. I'm assuming you have found the VBA editor, the alt-f11 shortcut may work.
Use the Names Manager to designate each of the three sentence locations as a "named range", e.g. first_sentence, second_sentence and third_sentence
Choose a shape and add it to your workbook. The shape will serve as the button. If you right click on the shape, at the bottom of the list of options, is one to connect a macro (which does not exist yet)
In the VBA code editor, make a new module, then create the macro by typing what is below. After creating it, you can connect it to your button, and that will allow you to test it.
Option explicit
' the macro "transfer_text" controls the export of text from the Excel sheet to the Word document
sub transfer_text()
' this is where clean-up code and everything goes
end sub
You need to tell your transfer_text macro where the cells of interest are, perhaps by pasting the following into the body of the macro:
dim range_first as range, range_second as range, range_third as range, clean_first as string, clean_second as string, clean_third as string
' set the ranges so the macro knows where the data is
with thisworkbook.worksheets("Sheet1")
set range_first = .range("first_sentence")
set range_second = .range("second_sentence")
set range_third = .range("third_sentence")
end with
' update the data (without changing the original) using the clean_text function (see below)
clean_first = clean_text(range_first)
clean_second = clean_text(range_second)
clean_third = clean_text(range_third)
' this is where we connect Word
I would use a function to clean the text in each of the ranges, like this, stored outside the main macro, though in the same module for convenience. You can add text editing operations to it later:
Function clean_text(string_text as string) as string
' the text from the range is passed in
' various operations performed on the text
' the result is sent back to where the function was called
clean_text = trim(string_text)
end function
That is rather alot already, and we have not even connected to Word. To connect to Word is really better done as a separate topic. You will need to set a specific reference to word, using the Tools-References menu in the VBA project. Scroll down the list until you find Microsoft Word, and tick the box.
That will give you access to the object library for Word, so you can place your text using bookmarks in the Word document.
You will also need to deal with the file object - opening the Word document. Another topic for you to research!
That should get you started!
Some notes on debugging. Set a breakpoint at the start of the With statement by clicking the mouse in the left hand margin of the code editor. Then click you button... the code will stop at the breakpoint. Use the mouse-over and the Immediate Window (you may need to unhide it) to check variable values. Press F8 to step forward one line of code. In the Immediate Window, type a ? followed by the variable you want to check.
I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".
I'm working on a macro that would allow me to type (or more likely, to paste) a SQL query into an InputBox and upon clicking "OK", it returns the results of the query against the database. The problem is, by default VBA only accepts one line of text and SQL code is written in a more structured, multi-line format for readability. When I try to paste SQL code into the InputBox, all but the first line of text are truncated. My next idea is to read the contents of the clipboard and replace any newline characters with a space before displaying the InputBox and prompting me to enter a query, but obviously that wouldn't help with queries which I am typing in rather than pasting. Any help would be greatly appreciated!
I don't believe you can do this in an InputBox. InputBoxes are generally meant for small inputs, like a number or word. Additionally, hitting enter while typing into an InputBox submits it, so it would be confusing if you could add multiple lines.
What you can do instead is create a new UserForm, and add a TextBox with the MultiLine field set to True. Then, just add an 'OK' button that closes the form, and have your macro read from the TextBox.
As another alternative, here's a function I use to get whatever text is currently on the user's clipboard. So, you could just copy your query prior to running whatever macro you need to use it in.
Function copyFromClipboard() As String
Dim clipboard As MSForms.DataObject
Set clipboard = New MSForms.DataObject
clipboard.GetFromClipboard
copyFromClipboard = clipboard.getText
End Function
It requires a reference to Microsoft Forms 2.0 Object Library
I am writing VBA macros in excel 2016. Macros I write that have arguments do not show up in the Macro Table, only the ones that have no arguments. Help.
Macros that take arguments are not visible in the macro box because there is no point in having them there. If they need arguments to run, they cannot be run from the macro box because there is no way to supply an argument to the macro in question.
Normally, a macro shows up in the macro list when you display the
Macros dialog box (press Alt+F8), unless one of three conditions is
met:
The macro is a function. Functions typically return information,
and they require information to be passed to them. Since running a
macro from the macro list doesn't allow either of these things to
happen, Excel figures there is no need to list it. User-defined
functions, which are quite useful in Excel, are not displayed in the
Macros dialog box because they are, after all, functions.
The macro is
a subroutine with parameters. Excel assumes that since parameters are
necessary, and you cannot provide parameters by choosing the
subroutine from the macro list, there is no need to list it.
The subroutine has been declared Private. This means that the subroutine
is only useful to code within the module in which it is declared.
Source.
Depending on your need, a possible workaround is to use a helper-sub like this:
Sub InvisibleMacro(strArg As String)
MsgBox("The passed argument was " & strArg)
' This macro won't be visible in the macro dialog because it can only be called with an argument
End Sub
Sub VisibleMacro()
Call InvisibleMacro("Abc")
' This macro will be visible in the macro dialog because it requires no arguments and is not private.
' It will call the "invisible" macro with a preset argument.
End Sub
You can use InputBox or the likes if you need the passed argument to be non-static. Of course, depending on what datatype you need to pass as an argument, this approach may be limited and/or require some extra hoops.
Obviously if the macro requires parameters they can't be passed by clicking an icon.
But I have a Sub that only has one Optional parameter and that doesn't show up in the list either when trying to attach it to an icon in the customized ribbon.
Comment out the full Sub declaration and substitute a line without parameters, attach the macro to an icon, then put back the real line. The icon will still work.
' Temporarily use the parameterless line to set things up, then put
' back the real line. I assume this would crash if the Sub has required
' parameters.
Sub MySub()
' Sub MySub(Optional ByVal MyParm As String)
If MyParm = "" Then MyParm = "No parameter"
MsgBox (MyParm)
End Sub
I know this is an older post but I had the same issue. My fix was that I had to open the macros in the VB editor and then they showed up in the macro list. from there I could add them to the ribbon and change the icons. Hope this helps.
The macro is not listed when it has parameters, but knowing the name you should be able to simply write the name and theirs parameters when Excel prompts in the Assign Macro window, using single and double quotes.
The following example will guide you how to do it:
'mymacro "param1"'
'mymacro TRUE, FALSE'
'mymacro "param1","param2"'
My problem was that I had defined Macro directly through Visual Basic Editor in Excel and had defined it under "Class Modules" folder instead of "Modules"
Once I moved it into Modules folder, it was visible.
Hope this helps someone.
You probably have made macro as "Function()". To make it visible in macros list, you have to declare it as "Sub()".