I'm trying to write code to hide rows between cells with exact color provided, and as far everything worked great I get an 1004 application-defined or object-defined error here:
Set c = c.Offset(1, 0) 'next row
In a screenshot I post how does my spreadsheet look. This code wasn't written by me but was taken from my another question, I just modified it a bit to a new idea. The code:
Sub showfour()
ToggleRows RGB(218, 238, 243), RGB(231, 238, 243), False
End Sub
Sub hidefour
ToggleRows RGB(218, 238, 243), RGB(231, 238, 243), True
End Sub
'Show or hide rows, beginning with a cell in ColB with fill color `clr`
' and ending with the first cell filled yellow or with no fill
' `HideRows` = True will hide, False will unhide.
Sub ToggleRows(clr As Long, lastcol As Long, HideRows As Boolean) 'clr - cell that marks the start, lastcol - ending cell
'every variable needs a type, unless you want a Variant
Dim v As Long, c As Range, ws As Worksheet
Set ws = Worksheets(1)
For v = 1 To ws.Cells(Rows.Count, "B").End(xlUp).Row
If ws.Cells(v, "B").Interior.Color = clr Then 'has the color of interest
Set c = ws.Cells(v, "B").Offset(1, 0) 'next cell down
Do While c.Interior.Color <> lastcol
Set c = c.Offset(1, 0) 'next row
Loop
v = v + 1
ws.Rows(v).Resize(c.Row - v - 1).EntireRow.Hidden = HideRows
MsgBox v
End If
Next v
MsgBox c
End Sub
The spreadsheet:
As you can see from a to c it works great but the d one is broken.
ToggleRowscalls are wrongly done, not supplying the second parameter (lastcol). Last row is extracted fomRGB(231, 238, 243)... Then, the called sub calculated the filled cells only based on B:B column. I cannot understand from the linked picture what range you try processing... Firstly, please addOption Expliciton top of the module. Then, a better way should be to build aUnionrange and hide all the rows at once, at the end.Set c = c.Offset(1, 0)- you've run off the bottom of the sheet.lastcolshould mean and where your code use it?