Using VBA to hide row and row below and VBA to unhide all aswell

anilsss

New Member
Joined
Oct 6, 2017
Messages
8
I have a range of Names in cell C19:C420 that I want to rank by sales, I have created a sheet that is showing all my data. Cells in C are merged together, for example, C19 and C20 are merged together to show a persons name. This carry's on up to the end of the data set.

the persons name in column C is derived from an IF statement. if it matches the criteria it will bring in the name, if not it will show "BLANK".

I need a VBA formula to go through the range C19:C420 in the sheet and hide cells with "BLANK" as well as the cell below it and then carry on through the range until it gets to the next blank and does the same until it gets to the end of the range.

Along with this I also need a VBA code to unhide all the hidden cells in that range with a click of a button I'm looking to place in the sheet.

I don't really know anything about writing a VBA code so the help would be grateful.

Thank you in advance.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try the following

Code:
Sub HideRows
  Dim iCntr As Long
    
    For iCntr = 420 To 19 Step -1    If Cells(iCntr, "C") = "BLANK" Then
    Rows(iCntr).EntireRow.Hidden = True
    End If
    Next

End Sub
 
Upvote 0
Alternatively this will toggle the hidden property of the rows
Code:
Sub HideRows()

  Dim Cnt As Long
    
    For Cnt = 19 To 420
        If UCase(Range("C" & Cnt).Value) = "BLANK" Then
            With Rows(Cnt).Resize(2).EntireRow
                .Hidden = Not .Hidden
            End With
        End If
    Next

End Sub
 
Upvote 0
I forgot the unhide button

Code:
Sub Macro1()Dim rngStart As Range


Set rngStart = ActiveCell
    With Cells
        .EntireColumn.Hidden = False
        .EntireRow.Hidden = False
    End With
    
rngStart.Select
End Sub
 
Upvote 0
Hi Guys,

Thanks for the quick response.

Truiz, I tried your VBA code and I receive a syntax error on the following line "For iCntr = 420 To 19 Step -1 If Cells(iCntr, "C") = "BLANK" Then" (without quotation marks).

Fluff, I tried yours too but it doesn't seem to do anything with the sheet.

Any help again would be great!

Thanks in advance!
 
Upvote 0
Forgot to mention, column C has a formula in there to bring back blank.

Not sure if this is affecting the macro:

=IFERROR(IF(INDEX(Sheet1!$T$2:$T$1000,A11,1)="PERSON",INDEX(Sheet1!$B$2:$B$1000,A11,1),"BLANK"),"BLANK")
 
Upvote 0
Fluff, I tried yours too but it doesn't seem to do anything with the sheet.
Not quite sure why it's not working for you. I've tried it with the formula you supplied in post#6 & it works fine for me.
Have you put he code in a standard module?
How are you running it?
 
Upvote 0
Additionaly, with my code, the first time it is run the rows should be hidden, the second time it is run the rows will become visible.
 
Upvote 0
Thanks Fluff,

My mistake, my data set started at row 9 not 19. I amended that bit of the code and now it looks like its working a treat, have no idea how its doing it but it's doing it. Thanks!
 
Upvote 0
Glad to help & thanks for the feedback

.Hidden is a property of the row & is always either False (if the row is visible) or True (if the row is hidden).
So this line
Code:
.Hidden = Not .Hidden
is swapping that value.
ie if the row is visible .hidden is False & therefore Not .hidden is True
HTH
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,217
Members
453,024
Latest member
Wingit77

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