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:

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
38,820
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
37,893
Office Version
  1. 2019
  2. 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
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,625
Messages
5,838,427
Members
430,547
Latest member
jopshio

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