Actions of Macro depend on the value of a cell

frankday

Board Regular
Joined
Apr 13, 2012
Messages
102
I am working with the following macros. I want to change the action of the macro depending on the value of a cell. If the cell = 1 then it should execute the second macro. If the cell = 2 then the macro should execute the first macro. Can anyone help.

Thanks in advance

Sub Callapes()
'
' Callapes Macro
'


'
Rows("5:11").Select
Selection.EntireRow.Hidden = True
Range("e12").Select
ActiveCell.FormulaR1C1 = "1"
Range("C12").Select

End Sub
Sub Expand()
'
' Expand Macro
'


'
Rows("4:12").Select
Selection.EntireRow.Hidden = False
Range("e12").Select
ActiveCell.FormulaR1C1 = "0"
Range("C12").Select
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
depending on the value of a cell.
What cell?
Do you want to run the macros automatically each time the cell is changed to either 1 or 2 ?
 
Upvote 0
What cell?
Do you want to run the macros automatically each time the cell is changed to either 1 or 2 ?

I want to connect this macro to a button. The goal is to click a button linked to a macro and have it hide select rows. Then click the same button again and unhide the same rows. I know how to assign a macro to a button.

Does that help?
 
Upvote 0
But what cell needs be checked to see if it contains 1 or 2 ?

Alternatively, if you just want to toggle between Callapes and Expand :
Code:
Sub Toggle()
If Rows("5:11").EntireRow.Hidden = True Then
    Call Expand
Else
    Call Callapes
End If
End Sub

Sub Callapes()
Rows("5:11").EntireRow.Hidden = True
Range("e12") = 1
Range("C12").Select
End Sub

Sub Expand()
Rows("5:11").EntireRow.Hidden = False
Range("e12") = 0
Range("C12").Select
End Sub
 
Last edited:
Upvote 0
That worked great. Thanks for the help.

But what cell needs be checked to see if it contains 1 or 2 ?

Alternatively, if you just want to toggle between Callapes and Expand :
Code:
Sub Toggle()
If Rows("5:11").EntireRow.Hidden = True Then
    Call Expand
Else
    Call Callapes
End If
End Sub

Sub Callapes()
Rows("5:11").EntireRow.Hidden = True
Range("e12") = 1
Range("C12").Select
End Sub

Sub Expand()
Rows("5:11").EntireRow.Hidden = False
Range("e12") = 0
Range("C12").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,249
Members
448,879
Latest member
oksanana

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