VBA for If X value exists, hide that row, 4 rows before, and 1 row after?

MrBartlett

New Member
Joined
Jul 22, 2018
Messages
25
Hi all,

I am wondering if someone could kindly help me with this problem.

If a cell contains value Z, I want to hide that row, X rows before, and Y rows after.

ThtJeP3.png


So, via the example above, for the range B1:B15, if a cell contains "#n/a" then hide row 14, rows 9:12, and row 15 (the highlighted rows). I want this to loop, where either we have it check every cell in B1:B15, or for it to check every nth cell, where n = 7.

Thanks in advance!
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Code:
Sub HidRows()


Dim Rloop As Long
Dim Xrows As Integer
Dim Yrows As Integer


Xrows = 2
Yrows = 3


'for the range B1:B15. uncomment "step 7" to go in steps of 7
For Rloop = 1 To 15 'Step 7
'If a cell contains value Z, I want to hide that row, X rows before, and Y rows after
    If Application.WorksheetFunction.IsNA(Worksheets("Sheet1").Range("B" & Rloop).Value) = True Then
        Rows(Rloop & ":" & Rloop).EntireRow.Hidden = True
        Rows(Rloop - Xrows & ":" & Rloop - Xrows).EntireRow.Hidden = True
        Rows(Rloop - Yrows & ":" & Rloop - Yrows).EntireRow.Hidden = True


    End If
Next Rloop


End Sub
 
Upvote 0
Code:
Sub HidRows()


Dim Rloop As Long
Dim Xrows As Integer
Dim Yrows As Integer


Xrows = 2
Yrows = 3


'for the range B1:B15. uncomment "step 7" to go in steps of 7
For Rloop = 1 To 15 'Step 7
'If a cell contains value Z, I want to hide that row, X rows before, and Y rows after
    If Application.WorksheetFunction.IsNA(Worksheets("Sheet1").Range("B" & Rloop).Value) = True Then
        Rows(Rloop & ":" & Rloop).EntireRow.Hidden = True
        Rows(Rloop - Xrows & ":" & Rloop - Xrows).EntireRow.Hidden = True
        Rows(Rloop - Yrows & ":" & Rloop - Yrows).EntireRow.Hidden = True


    End If
Next Rloop


End Sub

Thanks for the response! Any ideas why I get a type 13 error on this line?

Code:
Rows(Rloop - Xrows & ":" & Rloop - Xrows).EntireRow.Hidden = True
 
Upvote 0
Figured it out. It was trying to hide a row less than 0 lol.

I'm gonna go to bed and test this out in the morning.
 
Upvote 0

Forum statistics

Threads
1,215,694
Messages
6,126,254
Members
449,305
Latest member
Dalyb2

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