Macro help needed

imclean

New Member
Joined
Jul 31, 2002
Messages
1
I have a spreadsheet with 4 buttons (with macros 'attached') - say Macro1 in b1, Macro2 in b2, Macro3 in b3, Macro4 in b4...and I want to add a 5th button in b5 that randomly chooses between the 4 buttons above and makes them start.
I am an absolute novice in this regard...could someone please help.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Since it doesn't seem like you are going to get a solution, I will post one. (I pulled some some code from Chip Pearson about how to get the macro names.)

Note:
Before using these procedures, you'll need to set a reference in VBA to the VBA Extensibility library. In the VBA editor, go to the Tools menu, choose the References item, and put a check next to "Microsoft Visual Basic For Applications Extensibility" library. This enables VBA to find the definitions of these objects. If you are using Excel97, this library will appear in the References list without a version number: "Microsoft Visual Basic For Applications Extensibility". If you are using Excel 2000 or 2002, it will appear with a version number: "Microsoft Visual Basic For Applications Extensibility 5.3". It is very important that you reference the proper library. If you reference the wrong library, you will receive "Type Mismatch" errors. If you don't reference the extensibility library at all, you will receive "User Defined Type Not Defined Error".

Place your macros in module1 (i.e. Macro1, Macro2, Macro3, Macro4 from your post). Insert a new module and place the following code:
<pre>Sub RndChooseMacro()

Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim Macros As String
Dim ProcName As String
Dim x As Variant
Dim RndMacro As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("Module1").CodeModule
With VBCodeMod
StartLine = .CountOfDeclarationLines + 1
Do Until StartLine >= .CountOfLines
Macros = Macros & " " & .ProcOfLine(StartLine, vbext_pk_Proc)
StartLine = StartLine + _
.ProcCountLines(.ProcOfLine(StartLine, _
vbext_pk_Proc), vbext_pk_Proc)
Loop
End With

Macros = Application.Trim(Macros)
x = Split(Macros, " ")
RndMacro = Int((UBound(x) * Rnd) + 1)

Run x(RndMacro)

End Sub</pre>

Assign button 5 to RndChooseMacro.
 
Upvote 0

Forum statistics

Threads
1,214,528
Messages
6,120,064
Members
448,941
Latest member
AlphaRino

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top