Excel Macro help needed :: MrExcel Message Board


 FAQFAQ
   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   FavoritesFavorites   StatisticsStatistics 
 RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Online StoreOnline Store

MrExcel Message Board Forum Index -> Excel Questions

Macro help needed
Post new topic   Reply to topic
Last Thread | Next Thread  >   Printable version
  Author    Thread

imclean
Welcome to the Board


Joined: 01 Aug 2002
Posts: 1


Status: Offline

 Reply with quote  

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.

Post Thu Aug 01, 2002 10:53 pm 
 View user's profile Send private message

Al Chara
MrExcel MVP


Joined: 22 Feb 2002
Posts: 1428
Location: Monterrey, Mexico
Flag: Usa

Status: Offline

 Reply with quote  

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:


quote:
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:
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


Assign button 5 to RndChooseMacro.

_________________
Kind regards,

Al Chara

Post Fri Aug 02, 2002 12:21 am 
 View user's profile Send private message
  Display posts from previous:      

MrExcel Message Board Forum Index -> Excel Questions


Forum Jump:
Jump to:  

Post new topic   Reply to topic
Page 1 of 1



Add To Favorites

 


Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

Powered by phpBB: 2.0.4 © 2001 phpBB Group

Need help posting your first question? Read how to post

Need extra help ? Couldn't get the answer you needed ? Get a free quote from our Consulting Team

Download Colo's HTML Maker utility for displaying your Excel Worksheet on the board.

Download VB HTML Maker to post your code on the board


Check out our new index to 485 Excel Articles.


Return to MrExcel Consulting

All contents Copyright 1998-2004 by MrExcel.com
If you believe information posted here is from your copyrighted source, notify us per the Terms of Use
Excel is a registered trademark of the Microsoft Corporation.
MrExcel is a registered trademark of Tickling Keys, Inc.