The How-To Geek Forums Have Migrated to Discourse


How-To Geek Forums / Microsoft Office

Macro to to run diffent macro based on content of cell

(5 posts)
  • Started 3 years ago by Beau
  • Latest reply from moreeg
  • Topic Viewed 573 times

PARTNER SPOTLIGHT

Recommended: Clean Your Mac the Easy Way with CleanMyMac 3

If you want to clean up your Mac the easy way, CleanMyMac is a great tool to get the job done easily, and it will even keep your computer clean automatically.

Unlike the competition, it also includes great tools like a Clean Uninstaller, to get rid of applications and clean up the junk that they leave behind. All with the click of a button.

Download CleanMyMac 3 for Free Today

Posted yesterday 
Top
Beau
Posts: 2

Hi

In cell a1 there is a distinct content which will depend on the report that I have requested which is delivered to me as a excel spreadsheet. Lets say the content of a1 is "r121" or "r131". If it is "r121", I want it to run Sub Fortmat_r121, if it is r131 the I want it to run Sub Fortmat_r131.

Can someone assist with the code that I should use.....Thanks Mark

Posted 3 years ago
Top
 
moreeg
Posts: 842

Hi Beau

You can create a new macro that would have something like this

If range("1")="r121" then
run ("Format_r121")
else
run ("Format_r131")
End If

Enthusiast will be by in a minute to give a more developed response

Posted 3 years ago
Top
 
moreeg
Posts: 842

In the first line of the example code it should be Range("1A")

Posted 3 years ago
Top
 
Beau
Posts: 2

HI

Thanks for you reply

How would you add a third scenario to the below i.e. A1 = p121 then run "format_P131"

If range("A1")="r121" then
run ("Format_r121")
else
run ("Format_r131")
End If

Thanks

Posted 3 years ago
Top
 
moreeg
Posts: 842

Hi Beau
You can add as many as you want using "Else If" or if there is some consistent logic there we can be smarter e.g. if the numerical value of A1 is the determining factor and the alpha can be disregarded you can make you condition something like

If Right(Range("A1"), 3) = "131" Then
run ("Format_r131")
else
run ("Format_r121")
End If

or, something like this

BatType = Right(Range("A1"), 3)
run("Format_r" & BatType)

Depends on what could possibly populate A1 and what you want to do in each instance.

Posted 3 years ago
Top
 



Topic Closed

This topic has been closed to new replies.