SEARCH

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 1 year ago by Beau
  • Latest reply from moreeg
  • Topic Viewed 573 times

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 1 year ago
Top
 
moreeg
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 1 year ago
Top
 
moreeg
moreeg
Posts: 842

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

Posted 1 year 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 1 year ago
Top
 
moreeg
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 1 year ago
Top
 



Topic Closed

This topic has been closed to new replies.