I am trying to count the number of rows between areas in a collection (not sure if considered to just be a selection or a collection)
The code selects only the visible cells in the range specified. Each block of contiguous cells then makes up 1 area in the collection of areas.
I want to find the number of rows between each of these areas. (I need it for some other code i'm running not relevant to this)
Is it possible to work with the areas in my collection outside of the For Each loop. Like reference the area index somehow IE: could I find the last row of area 1 and the first row of area 2 and then take the difference between the two to get my count of rows between the two areas or do i individually have to loop through each area
something like this
area1lastrow = area1.row + (area1.rows.count - 1)
area2row = area2.row
rowbetween = area2row - area1lastrow - 1
run my other code
or is it possible to select the non visible cells in the range (like a non visible equivalent of the specialcells(xlcelltypevisible) command or some other method) and create a collection of areas made up of each block of non visible cells so I can then count the rows in each of those areas
something like this
For Each nonVisibleAr in activecell.Range("A1:A30").SpecialCells(xlcelltypenonvisible).areas
rowbetween = nonVisibleAr.rows.count
run my other code
Next
The current method i am using is just running a loop that checks if each cell is hidden and if hidden increments the rowsbetween up to get the rows between areas. It works but i'm trying to avoid loops if possible and wanted to see if there was some cleaner method to do this.
Here is that code for reference or see the attached file (module 1):
Option Explicit
Option Base 0
Sub CheckAreas()
Dim DataBlock As Range
Dim rowoffset As Long
Dim areacount As Long
Dim areaschecked As Long
Dim rowsbetween As Long
Range("A2").Select
areacount = ActiveCell.Range("A1:A30").SpecialCells(xlCellTypeVisible).areas.Count
For Each DataBlock In ActiveCell.Range("A1:A30").SpecialCells(xlCellTypeVisible).areas
areaschecked = areaschecked + 1
rowoffset = DataBlock.Rows.Count
rowsbetween = 0
If areaschecked < areacount Then
Do While DataBlock.Offset(rowoffset, 0).Range("A1").EntireRow.Hidden = True
rowoffset = rowoffset + 1
rowsbetween = rowsbetween + 1
Loop
End If
MsgBox "DataBlock Address " & DataBlock.Address(RowAbsolute:=False, columnAbsolute:=False) _
& " area " & areaschecked & " has " & DataBlock.Rows.Count & " Rows" & vbCrLf _
& "And There are " & rowsbetween & " Rows between area " & areaschecked & " and the start of the next area"
Next
End Sub
Bookmarks