Checking if a macro is called in another macro - vba

I have a lot of macros in my excel. I don't know whether they are used in that workbook. I don't know whether it is called as a procedure inside other macros.
Is there any way to find out whether the macro is used inside another macros or used in different sheets?

Probably the easiest way - Ctrl + F.
Write the name of the "Macro" and select search in Current Project. Then start counting how many times it will show up.
Another way is to write debug.print "I am used" after the Sub line of the "Macro". Then count how many times has it popped up on the immediate window.

put “Option Explicit” at the beginning of every module
comment out your “Macro
start debug
And it’ll point you to any occurrence of “Macro” call

Related

Is it possible to copy the contents of the Immediate window in Excel VBA?

Question
I would like to know whether it is possible to copy or extract the contents of the Immediate window in Excel VBA, so I can store it somewhere. I already know you can copy and paste from the window manually, I am interested in whether it is possible to do it with code (preferably in VBA itself).
Explanation
Questions similar to this have been asked a few times on SO (e.g. this and this), no-one has given a definitive answer and I was unable to find one from a quick search. Most of the time, answers respond asking why anyone would want to do that, and give ways to get around it in the circumstances provided in the question (e.g. how to output messages to a file or cell range). Given this, I have thought of a couple of example scenarios where someone might want to do this and cannot (easily) get around it.
Example scenarios
A) I am developing a large workbook including a large set of macros, debugging as I go. This includes using debug.print statements and directly querying in the Immediate window, e.g. ? myVar. At the end of the session, I would like to be able to automatically copy the contents of the immediate window into a log file, so I know what happened during my debug session afterwards. This could, for example, make use of the Workbook_BeforeClose event procedure.
B) I am working with two workbooks that contain VBA projects - one I can't edit, another that I can and am working on. The one I can't edit outputs debug information to the immediate window via debug.print that I would like to store somewhere (but I don't really care where).
Disclaimer
I ask this question purely out of curiosity. I suspect I already know the answer (it's not possible to do this), but I am not sure.
Yes-- but not with control-c .... select what you need and then drag and drop
1-Create a sheet named "debug.print"
2-Hide it:
Sheets("debug.print").Visible = 2 'xlSheetVeryHidden, only can be visible with vba
3-Create this function:
Function debug_print(c As String)
ThisWorkbook.Sheets("debug.print").[a1048576].End(xlUp).Offset(1).Value = c
Debug.Print c
End Function
4-Replace your codes "debug.print" to "debug_print"
5-Example:
Sub blablabla()
debug_print "i am doing whatever"
End Sub
But: Using "?" in the immediate window will not save
There is many ways to export this as file now like CSV, TXT, save in SQL etc...

Determine whether a macro is recorded and has not be modified by people

I want to determine if a macro is recorded by Excel "Record Macro", and if possible, if it has been modified by people.
There are some clues about it. For instance,
1) a recorded macro ALWAYS starts by
Sub m()
'
' m Macro
'
'
first line
...
End Sub
(Note that sometimes, user name may be recorded after m Macro)
2) a recorded macro ALWAYS appear in a standard module
3) a recorded macro NEVER has a variable, a condition, a loop, or a procedure call
From the syntax of the sub, we can not be 100% sure to say a macro is recorded, because we can always follow word by word and write manually a same function. We cannot be 100% sure if a recorded macro is not modified, because people can well manually change a value or an index. However, are there more clues (like what I listed) to improve this confidence percentage?
Otherwise, is there any other mechanism than syntax to determine this?
Short answer: No
I could write a sub to look exactly like a recorded one - so you wouldn't know the difference.
Similarly, a less experienced user might record macros and then copy/paste the bits they need into another macro - so it would still look like a recorded macro.
The nearest thing you could have to this is an event driven macro to track any changes in the VBProject for the workbook - but anyone working in the VBE will more than likely have the know-how to get around that anyway.

Excel thinks Combo Box is clicked when sheet is calculated in VBA

I have a number of private subs in Sheet code that refer to ComboBoxXX_Click. If I run another macro in the same Sheet that has SheetX.Calculate anywhere in the macro (referring to the sheet that contains the ComboBox) in the VB Editor using the step-into function, when it comes to execute the .calculate line it jumps to the ComboBoxXX_Click Macro. This makes me think that Excel is thinking that the ComboBox is clicked when the sheet is recalculated.
Try a different option instead of ComboBoxXX_Click
I suggest looking at the list of events that can be triggered on MSDN for that control.
try MouseDown
If you could add more information and blocks of code to help us help you. Also have you stepped through your code to find out at what point it is calling the ComboBoxXX_Click?
enter image description here
Here is an example of what I mean. Once the step into hits the Sheet4.Calculate on the first macro it skips to the second one (ComboBox25_Click) and if I step into the second one, once it reaches the Sheet4.Calculate line it also skips to a random ComboBoxXX_Click Macro.

Excel VBA: Resetting spreadsheet count

I have a excel VBA macro that dynamically generates and deletes spreadsheets based on user input. However, when I open the VBA IDE, it seems that although I am naming my spreadsheets in the subs that create/delete them, the overall count is still increasing.
For example, depending on how far into execution my program is, under the "Microsoft Excel Objects" folder in my current project, the spreadsheets in the current workbook could look something like
Sheet101(Sheet3)
Sheet103(Sheet2)
Sheet104(Sheet1)
Or
Sheet81(Inputs)
Sheet83(Date Adjustment Interpolation)
Sheet84(Pricing)
Sheet85(Comparison)
No matter if I delete the rest of them and add one, it still picks up where the last highest one left off.
I don't know how many times this macro will be run and I'd feel a lot better about putting it out there if I could reset this annoying tally on the number of spreadsheets that have ever been generated, since I don't know for sure where excel will cut me off. Plus it's just annoying.
My Question:
I would like to know how to alter that spreadsheet number, or at least what the relevant object is for doing so.
Thanks!
Thanks to #dijkay s suggestion on code names, I've found some code to accomplish this.
ThisWorkbook.VBProject.VBComponents("Sheet1").name = "test"
Will change the code name of Sheet1 to test, so in the Excel Objects folder, it will appear as test(Sheet1) for example.
This option, however, requires messing around with some trust/security settings in each individual excel client running the macro, which is unsuitable for my purposes, unfortunately. You can also change the value manually by changing the (Name) property directly in the IDE through the properties window.
here are some ideas you can try...
Sheets(x).Name = "Sheet" & x
or (assuming in this example, 'Sheet3' doesn't already exist:
Set Sheet3 = sheets.Add
Sheet3.name = "Sheet3"
This is more cleanup than re-setting
cheers,
Micéal

Excel VBA module subroutine not being picked up with parameter

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.