powerpivotlegal
New Member
- Joined
- May 14, 2014
- Messages
- 30
Hello,
I am extremely limited in my knowledge of VBA macros and cannot seem to find the exact solution on searching through forum posts.
I am trying to write a macro that hides the row when you mark the cell in the last column of the named table range. For example, Table Name = "Ben" for Columns A2 through J20. If J4 is marked with an "X" then hide row 4. Hide row 5 if J5 is marked with an "x", etc.
This macro will need to be continuously running so that it incorporates any new rows of data added to the named table range.
The best I could cobble together is the macro below, but it runs super slow because I had to define the range so large in order to account for possible new row additions. How can a macro just look to the range of data in just one column of the named table range?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Range("j2:j20000")
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
For Each c In r
If c = "X" Or c = "x" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next c
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Many thanks in advance.
Regards,
James
I am extremely limited in my knowledge of VBA macros and cannot seem to find the exact solution on searching through forum posts.
I am trying to write a macro that hides the row when you mark the cell in the last column of the named table range. For example, Table Name = "Ben" for Columns A2 through J20. If J4 is marked with an "X" then hide row 4. Hide row 5 if J5 is marked with an "x", etc.
This macro will need to be continuously running so that it incorporates any new rows of data added to the named table range.
The best I could cobble together is the macro below, but it runs super slow because I had to define the range so large in order to account for possible new row additions. How can a macro just look to the range of data in just one column of the named table range?
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, c As Range
Set r = Range("j2:j20000")
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
For Each c In r
If c = "X" Or c = "x" Then
c.EntireRow.Hidden = True
Else
c.EntireRow.Hidden = False
End If
Next c
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
Many thanks in advance.
Regards,
James