Tested in Excel 365 (16.8730.2046) 64-bit
- Public Function LastRowOrCol(Optional IsColumn = False, Optional StartPos = 1, Optional NbIterations = 1)
- Dim MaxRowCol, i
- MaxRowCol = 1
- If IsColumn Then ' get the last column
- For i = StartPos To StartPos + NbIterations
- If MaxRowCol < Cells(i, Columns.Count).End(xlToLeft).Column Then _
- MaxRowCol = Cells(i, Columns.Count).End(xlToLeft).Column
- Next i
- Else ' get the last row
- For i = StartPos To StartPos + NbIterations
- If MaxRowCol < Cells(Rows.Count, i).End(xlUp).Row Then _
- MaxRowCol = Cells(Rows.Count, i).End(xlUp).Row
- Next i
- End If
- LastRowOrCol = MaxRowCol
- End Function
- Sub example()
- ' get the last row checking 7 rows starting from row 1
- Debug.Print LastRowOrCol(, 1, 7)
- ' get the last column checking 5 rows starting from column 1
- Debug.Print LastRowOrCol(1, 2, 5)
- End Sub