Find range of blank rows and hide them, within a worksheet change event

cdown16

New Member
Joined
Jan 5, 2012
Messages
5
Hello, I am giving up and posting as I cannot get the second part of this code to work within a Worksheet change, but it works when placed in a separate macro that I manually call. I need this whole code to run during a cell change. The Pivot Table piece works, everything after with hiding/unhiding rows does not.

I am finding a range of blank rows between two pivot tables and wanting to hide those rows. As we know, pivot table row counts vary on selection so it has to stay dynamic, but then unhide those rows before it starts searching for blank rows again. Below the dotted line is where I need some help on why within a worksheet change, is this part not running. THANKS!



Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'This line stops the worksheet updating on every change, it only updates when cell
'is touched
If Intersect(Target, Worksheets(AMPS).Range("C388")) Is Nothing Then Exit Sub


'Set the Variables to be used
Dim pt As PivotTable
Dim Field As PivotField
Dim NewCat As String
Dim rngstart As Range
Dim rnghide As Range
Dim lcell As String
Dim ecell As String



'Here you amend to suit your data
Set pt = Worksheets(AMPS).PivotTables("PivotTable3")
Set Field = pt.PivotFields("Category New")
NewCat = Sheets("AMPS").Range("c388").Value


'This updates and refreshes the PIVOT table
With pt
Field.ClearAllFilters
Field.CurrentPage = NewCat
pt.RefreshTable
End With


-------------------------------------------------------------Need help below.
Sheets("AMPS").Rows("5:386").Hidden = False


Set rngstart = Range("a5")
Set rnghide = rngstart.End(xlDown).Offset(1, 0)
lcell = rngstart.End(xlDown).Offset(1, 0).Row
ecell = rnghide.End(xlDown).Offset(-1, 0).Row

If NewCat = "COLD CEREAL" Then
Sheets("AMPS").Rows(lcell & ":" & ecell).Hidden = True
ElseIf NewCat = "FROZEN BREAKFAST" Then
Sheets("AMPS").Rows(lcell & ":" & ecell).Hidden = True
ElseIf NewCat = "PWS" Then
Sheets("AMPS").Rows(lcell & ":" & ecell).Hidden = True
ElseIf NewCat = "*******" Then
Sheets("AMPS").Rows(lcell & ":" & ecell).Hidden = True
ElseIf NewCat = "TOASTER PASTRY" Then
Sheets("AMPS").Rows(lcell & ":" & ecell).Hidden = True
Else
Sheets("AMPS").Rows(lcell & ":" & ecell).Hidden = False
End If


End Sub
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
I even tried to really simplify it and use:

Range("A6:A386").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True

and no rows hide in the event either. Any help is greatly appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,867
Messages
6,122,002
Members
449,059
Latest member
mtsheetz

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