перед тем, как использовать
, вы должны определить размер emptyRows()
, кроме того, вы можете использовать WorksheetFunction.Count()
для проверки любого значения в текущей строке
finally
Function FindAllEmptyRows(sheet As Worksheet) As Variant
Dim emptyRows() As Variant
Dim i As Long, rowNumber As Long, rowCounter As Long
With sheet.UsedRange ' reference passed sheet UsedRange
rowNumber = .Rows.Count
ReDim emptyRows(0 To rowNumber - 1) ' dim the array to the maximum possible size
For i = rowNumber To 1 Step -1 ' step through reference range rows from the last baxkwards to the first
If WorksheetFunction.Count(.Rows(i)) = 0 Then
emptyRows(rowCounter) = i + .Rows(1).Row - 1 ' fill array in current index with current row index
rowCounter = rowCounter + 1 ' update array index
End If
Next
End With
ReDim Preserve emptyRows(0 To rowCounter) ' redim the array according to the actual number of found empty rows
FindAllEmptyRows = emptyRows
End Function
обратите внимание, что:
emptyRows(rowCounter) = i + .Rows(1).Row - 1
хранит индекс строки absolute , т. е. индекс строки листа, тогда как
emptyRows(rowCounter) = i
будет хранить индекс строки relative , т. е. индекс строки с UsedRange
, который может начинаться с строки, отличной от строки 1