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

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,272
Members
448,558
Latest member
aivin

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