Need help with change event

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
Hi all,

I have several different macros in a event change. I would like to have different macros run, based on certain cells that change.

Example:

This code I want to run when any cell is changed:

Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("Distro").Calculate

And this code to run only when cell F1 is changed:

If Range("GF3").Value = "2" Then 'Distro1x12
Rows("13:52").EntireRow.Hidden = True
ElseIf Range("gf3").Value = "6" Then
Rows("13:52").EntireRow.Hidden = False
End If

And this code to run only when cell F54 is changed:

End If
If Range("GF56").Value = "2" Then 'Distro2x12
Rows("66:105").EntireRow.Hidden = True
ElseIf Range("gf56").Value = "6" Then
Rows("66:105").EntireRow.Hidden = False

The combined code is below.


Private Sub Worksheet_Change(ByVal Target As Range)
Worksheets("Distro").Calculate
If Range("GF3").Value = "2" Then
Rows("13:52").EntireRow.Hidden = True
ElseIf Range("gf3").Value = "6" Then
Rows("13:52").EntireRow.Hidden = False
End If
If Range("GF56").Value = "2" Then
Rows("66:105").EntireRow.Hidden = True
ElseIf Range("gf56").Value = "6" Then
Rows("66:105").EntireRow.Hidden = False
End If

End Sub

Thank you!

Russ
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi,

Try this:

Code:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
    If Not Intersect(Target, Target.Worksheet.Range("F1")) Is Nothing Then
        If Range("GF3").Value = "2" Then 'Distro1x12
        Rows("13:52").EntireRow.Hidden = True
        ElseIf Range("gf3").Value = "6" Then
        Rows("13:52").EntireRow.Hidden = False
        End If
    ElseIf Not Intersect(Target, Target.Worksheet.Range("F54")) Is Nothing Then
        If Range("GF56").Value = "2" Then
        Rows("66:105").EntireRow.Hidden = True
        ElseIf Range("gf56").Value = "6" Then
        Rows("66:105").EntireRow.Hidden = False
        End If
    End If
[COLOR=#333333]Worksheets("Distro").Calculate[/COLOR]
End Sub
 
Last edited:
Upvote 0
Try this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This code I want to run when any cell is changed:
    Worksheets("Distro").Calculate
    
    'And this code to run only when cell F1 is changed:
    If Not Intersect(Target, Range("F1")) Is Nothing Then
        If Target.Count = 1 Then
            If Range("GF3").Value = "2" Then
                Rows("13:52").EntireRow.Hidden = True
            ElseIf Range("gf3").Value = "6" Then
                Rows("13:52").EntireRow.Hidden = False
            End If
        End If
    End If
    
    'And this code to run only when cell F54 is changed:
    If Not Intersect(Target, Range("F54")) Is Nothing Then
        If Target.Count = 1 Then
            If Range("GF56").Value = "2" Then
                Rows("66:105").EntireRow.Hidden = True
            ElseIf Range("gf56").Value = "6" Then
                Rows("66:105").EntireRow.Hidden = False
            End If
        End If
    End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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