Condensing VBA code

keelybin

New Member
Joined
Jun 30, 2014
Messages
37
Hi folks!

I have the following code which I don't want to have to copy 96 times for the 96 rows it affects. Can someone please help me condense it?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Row_int As IntegerDim
Check_Rng As RangeDim 
N_int As IntegerDim 
Hide_row_int As Integer
Application.ScreenUpdating = False
Select Case Target.Address
Case "$A$16"
If Target.Value = "/TP" Then
Rows("17").Hidden = True        
Else            
Rows("17").Hidden = False        
End If
End Select

Application.ScreenUpdating = True
End Sub

The code would have to apply to each row (from A16) to hide the row underneath it, if the said cell contains the text "/TP"

I'm assuming a loop would be the best option, but I've no idea how to write code for this.
 
Last edited:

Some videos you may like

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,528
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Something like this:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rngCell               As Range

    If Not Intersect(Target, Range("A16:A112")) Is Nothing Then

        Application.ScreenUpdating = False
        For Each rngCell In Intersect(Target, Range("A16:A112")).Cells
            If rngCell.Value = "/TP" Then
                rngCell.Offset(1).EntireRow.Hidden = True
            Else
                rngCell.Offset(1).EntireRow.Hidden = False
            End If
        Next rngCell
        Application.ScreenUpdating = True
    End If
End Sub
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,891
Office Version
  1. 2010
Platform
  1. Windows
I believe this non-looping macro will also do what you want...
Code:
Sub HideCertainRows()
  With Range("A16", Cells(Rows.Count, "A").End(xlUp))
    .EntireRow.Hidden = False
    .Replace "/TP", "#N/A", xlWhole
    .SpecialCells(xlConstants, xlErrors).Offset(1).EntireRow.Hidden = True
    .Replace "#N/A", "/TP", xlWhole
  End With
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,128,069
Messages
5,628,468
Members
416,319
Latest member
k8o2

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
Top