Excel Macro: Run Macro Based on a cell value

rorymcc

New Member
Joined
Jul 7, 2017
Messages
9
Hi I'm trying to run a macro based on another cell value. I simply want to hide rows based on a dropdown value from another cell. Is this possible. I have working code that is hiding rows, but it is not being triggered when the value is selected I have to manually run the macro. Could someone help me?

Code:

Private Sub worksheet_change(ByVal target As Range)
Set target = Range("B1")
If target.value = "Delete" Then
Call Macro1
End If
End Sub


Sub Macro1

If Range("B1").Value="Delete" Then
Rows("3:4").EntireRow.Hidden = True .......etc


The sub macro1 is working but not being triggered by the private sub on the worksheet
 
Do you know why S_Wish code would provide an error using the Case statement?
Yes.
It needs to be
Code:
Case Else
not
Code:
Else
 
Upvote 0

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
I'm going to be a pain on this forum I think, but looking to learn VBA and improve.

I've just changed something that means a value of the cell is as a result of a formula. However the macro doesn't run from the result of a formula. However if a type in the cell the result it does work. How can i change it so it runs from a result of a formula?
 
Upvote 0
Worksheet_Change procedures fire on the manual change of a cell. If the change is the result of a formula change, it will not fire a Worksheet_Change procedure, based on that cell.
There is a Worksheet_Calculate procedure which fires when a formula calculation occurs, but you cannot limit it to a particular cell, meaning it fires whenever any calculation on the sheet is updated (often overkill).

What I often like to do, if possible, is look at the formula in B1, and have the Worksheet_Change procedure fire based on changes to the cells that the formula calls.
So what is the formula?
 
Upvote 0
Hi Joe,

Formula is =IF(COUNTIF(C8:C12,"Yes")>=1,"Yes","False")

So want B1 to run the Macro if the result is Yes and unhide the rows if anything else/no.
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Range("C8:C12"), Target) Is Nothing Then
        If Range("B1") = "Yes" Then
            Rows("3:4").EntireRow.Hidden = True
        Else
            Rows("3:4").EntireRow.Hidden = False
        End If
    End If
End Sub
The key line is this here:
Code:
If Not Intersect(Range("C8:C12"), Target) Is Nothing Then
What it essentially says is "Is the cell being updated in the range C8:C12"?
 
Upvote 0
You're a genius
I don't know if I would go that far! There are users here who blow me away with some of the stuff they do.
Let's just say I am "very experienced"!;)
 
Upvote 0
does anybody know how i would run two macros within one worksheet change. My code ive tried so far hasn't worked for the second macro but is running the first one:

Private Sub Worksheet_Change(ByVal Target As Range)
My_Sub_A Target
My_Sub_B Target


End Sub


Public Sub My_Sub_A(ByVal Target As Range)
If Not Intersect(Range("C16:C21"), Target) Is Nothing Then
If Range("C22") = "Yes" Then
Rows("23:25").EntireRow.Hidden = True
Else
Rows("23:25").EntireRow.Hidden = False
End If
End If
End Sub

Public Sub My_Sub_B(ByVal Target As Range)
If Not Intersect(Range("F2:F3"), Target) Is Nothing Then
If Range("F4") = "Yes" Then
Rows("27:28").EntireRow.Hidden = True
Else
Rows("27:28").EntireRow.Hidden = False
End If
End If
End Sub
 
Upvote 0
You would do it like this:
But you had:
C16:C21
But then referred to C22 which is outside the range
You did this in both codes. I changed them both.


Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("C16:C22"), Target) Is Nothing Then
If Range("C22") = "Yes" Then
Rows("23:25").EntireRow.Hidden = True
Else
Rows("23:25").EntireRow.Hidden = False
End If
End If
If Not Intersect(Range("F2:F4"), Target) Is Nothing Then
If Range("F4") = "Yes" Then
Rows("27:28").EntireRow.Hidden = True
Else
Rows("27:28").EntireRow.Hidden = False
End If
End If
End Sub
 
Upvote 0
I would think both codes should be like this:
See code change marked in red.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("[COLOR=#ff0000]C22[/COLOR]"), Target) Is Nothing Then
If Range("C22") = "Yes" Then
Rows("23:25").EntireRow.Hidden = True
Else
Rows("23:25").EntireRow.Hidden = False
End If
End If
If Not Intersect(Range("[COLOR=#ff0000]F4[/COLOR]"), Target) Is Nothing Then
If Range("F4") = "Yes" Then
Rows("27:28").EntireRow.Hidden = True
Else
Rows("27:28").EntireRow.Hidden = False
End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,207
Members
452,618
Latest member
Tam84

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