In a new excel spreadsheet, I have inserted a new module and entered a public sub. When trying to test, the sub does not appear in the macro list unless I remove the parameter.
This behaviour is bizarre and I cannot find any reference to it as an issue, everything I have read declares that subroutines (or functions, tried that too) can have parameters.
Public Sub RetrieveSIR() <-- Can be found
Public Sub RetrieveSIR( SIRNumber as Integer) <-- Cannot be found
It is driving me around the bend trying to work this out. If anyone can help, it would be much appreciated.
Subs with parameters won't show up in the macro list for the same reason you can't simply run a sub with parameters from the vba editor screen. They can only be called via code so the parameter required can be input.
Edit:
If for whatever reason you really need your macro to be in that macro list, you should make that parameter a variable in your macro and use an inputbox to specify it. That way when the user clicks the macro, they'll be prompted for the input and then the macro can run accordingly.
With regards to functions, you can have a function with parameters and use it as a formula in excel but as far as I'm aware they won't show up in the macro list either.
Related
(SW versions: Excel 2010 - Microsoft Date and Time Picker Control 6.0 (SP4))
In the sheet "sheet1" I have an ActiveX Date Picker control calendar named "foobarCalendar".
Next to it I put a button and in its "Assign Macro..." I want to specify a macro with as argument the date of this calendar "foobarCalendar".
In VBA I can get this date with "sheet1.foobarCalendar", however I'm unable to find any working way to put this as argument in the "Assign Macro..." window.
Is there a working syntax or it's simply not possible?
(another problem I encounter is that my macro name doesn't appear on the list, but I discovered that it's because Excel only display the macroes that don't get arguments in this list, although I can manually write them and use them successfully)
I tried for example 'macroName sheet1.foobarCalendar' but it's not working.
I also tried many other syntax, including those that work for passing named ranges and including the "evaluate" variants, without success. Is it possible?
In VBA I can get this date with "sheet1.foobarCalendar", however I'm unable to find any working way to put this as argument in the "Assign Macro..." window.
You don't. Sheet1.foobarCalendar is already in scope: have the macro use it.
Public Sub MyMacro()
Dim workingDate As Date
workingDate = Sheet1.foobarCalendar
'...
End Sub
That said, there is a way to invoke a parameterized macro, but as far as I could get things to work, it doesn't work with a variable argument, which appears to be required to be a hard-coded value - the syntax requires enclosing the "macro name" with single quotes, adding a space before the argument, like this:
'Test "test"'
Correctly invokes this macro:
Public Sub Test(ByVal value As String)
MsgBox value
End Sub
But, if a macro needs to use data that exists on the worksheet at the time of invocation, there's no need to parameterize it. And if a macro needs to prompt the user for a date, a path, or anything, ...then it can absolutely do that.
Note that the date picker control will likely not work properly on a 64-bit host: Consider using another way to prompt the user for a date, that doesn't involve 32-bit ActiveX tech.
I have a text box which takes user input which is referenced by a number of custom functions in VBA. I wanted the functions to recalculate everytime the input in the text box is changed. The text box input represents a scale in the format 1:24 which is used in the functions and converted to a decimal fraction.
The code works well initially but then stopped working when I reopened it. If i add a message box after the calculate. I had the same issue with using SelectionChange for the worksheet. Thoughts? Here is the code that I used.
Also I am using a formatted table which is what is not updating.
Private Sub ScaleBox_Change()
Worksheets(ActiveSheet.Name).Calculate
End Sub
Your event handler ScaleBox_Change does not make any changes to the worksheet, so any calculation will be working on the old data.
To fix this, place the result from the ScaleBox and place it in the worksheet in the appropriate place (and in the right format for what you are trying to do). Then do the calculate routine.
Try something like:
Private Sub ScaleBox_Change()
Applicatin.CalculateFull '<<< edit
End Sub
If that doesn't do it then it would help to post your formulas.
I have a workbook that used to be one with many sheets, I have now split the sheet to different workbooks.
The problem I now have is userform population from cell selection. When all the sheets were together. This code worked great.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not (UFMJobSelectForm.ActiveControl Is Nothing) Then
Call UpdateJobSelectForm
End If
End Sub
However now the Userform is in one workbook and this code is in another. I don't want to reference library as I need it to open and close so it can be accessed for other people to use.
Thanks for any help in advance.
Edit:
What i have is 4 different workbooks with jobs on and i want to select the job and the userform populates with job details.
The code I have detects the userform is open and then calls then populates the userform using updatejobselectform. Which worked when all the sheets were in the same workbook. however no longer works now i have separated them.
When i run this code now the sheets are in there own work books i get Error: runtime erroe 424 object required.
So what i am asking is dose anyone know how i can check a userform is loaded from in a different workbook and how i can get the useform to interact with cell selection from a different workbook.
thanks again.
This looks like a code that primafacie must fire when selection change event occurs, and call a function that must update the said form.
If the code and the said form are dependent, while splitting up should they not be together.
In case there are some other constraints that do not let you do so, and are not defined in the question do feel free to update the question / comment below.
Happy to help!
I know that I can write the following code in a sheets' VBA-object to run code on a worksheet change.
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Is there anything similar that I can write to run code whenever I filter a certain ListObject?
Only in some cases. Say we include a new column in the table that we fill with the value 1. Elsewhere we insert an
=SUBTOTAL()
formula to sum that column. As the filter is operated the number of visible rows will vary. The SUBTOTAL() function will re-calculate.
At this point a Calculate Event macro would catch the re-calculation!
Try this...
I added a ListBox Form Control to my Excel sheet..
Next, I assign a macro to this object by Right Clicking and choosing "Assign Macro". (you may need to be in "Design Mode" to make this happen -- check the Developer Ribbon)
By default - the Macro Name is populated with a change event macro name. Click "New" to create the macro.
Your macro is added to a Module. Hope this helps!
Is this possible in Excel?
I have a workbook with multiple worksheets. I wrote some vba code in a code module to shell to an exe and pass it cell values as arguments.
What I want to be able to do is select a cell or row in any of my worksheets and then call my Shell Sub while passing the values from a couple cells to the Sub. A hot-key combination would be best.
The part I am having trouble with is calling a sub in a code module from a hot hey.
Can you help with this? Any sample code?
Much appreciated!
Found my answer! It was in a post from Leith Ross found here...
http://www.excelforum.com/excel-programming/590157-keyboard-shortcut-to-run-sub-module-in-vb.html
If your macro is a Sub then its easy to assign a shortcut key to your macro. While in Excel, type ALT+F8 to bring up the Macros List. Select the name of your macro by clicking it. At the bottom right corner of the dialog, click "Options..". You can then assign a shortcut key and add a description of the macro for other to see when it is selected in the Macro List.
That's exactly what I needed.