vba calling a macro based on cell value

tommyleinen

Board Regular
Joined
Aug 22, 2009
Messages
74
Hello, I wonder if anyone can advise on if the following is possible? I have a sheet with various weeks' data, and there is a small macro to sort by week for each week in the sheet. So there are macros: sort1, sort2, sort3, sort4 etc.

What I would like to do is call the relative macro within another macro based on a cell value. EG A1 =1, then call sort1; If A1 = 2, then the part in the VBA would call sort2. etc. I don't want the macro called when the cell is changed, just to be able to write the code which I think might be along the lines of:

Code:
call sort & Sheets("Sheet1").Range("A1").Value

Is something like this possible?

Thanks in advance !
 

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.
Hello.
Try:
Code:
Sub callsort()
 Dim myMacro As String
  myMacro = "sort" & [A1]
  Run myMacro
End Sub
 
Upvote 0
Hello.
Try:
Code:
Sub callsort()
 Dim myMacro As String
  myMacro = "sort" & [A1]
  Run myMacro
End Sub


Hi Osvaldo, thanks for your reply. I will try this when I get a chance later. How would I integrate that to an existing macro?

Do I make the declarations:

Dim myMacro As String
myMacro = "sort" & [A1]

At the top of the existing macro and then use "Run myMacro" whenever needed?

Thanks
 
Upvote 0
So you have 52 separate macros for each week of the year? I would like to see your macro for sorting.
 
Upvote 0
So you have 52 separate macros for each week of the year? I would like to see your macro for sorting.

Haha not quite, but yes they are seperate macros, I probably could have simplified them but I didn't anticipate the sheet growing arms legs and siblings. There are 17 sort macros.
 
Upvote 0

Forum statistics

Threads
1,216,127
Messages
6,129,024
Members
449,482
Latest member
al mugheen

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