I am using the following functions to determine the highest row and column that contain data:
Public Function LastColumn(ws As Worksheet) As Long
Dim LColumn As Long
If Application.CountA(ws.Cells) > 0 Then
LColumn = ws.Cells.Find(What:="*", _
after:=ws.Range("A1"), _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious).Column
Else
LColumn = 0
End If
LastColumn = LColumn
End Function
Public Function LastRow(ws As Worksheet) As Long
Dim lrow As Long
If Application.CountA(ws.Cells) > 0 Then
lrow = ws.Cells.Find(What:="*", _
after:=ws.Range("A1"), _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Else
lrow = 0
End If
LastRow = lrow
End Function
I've just inherited a spreadsheet which contains merged cells and it would seem these merged cells cause the above functions to fail, on the lines in bold, with a Run-time error '91' "Object variable or with block variable not set".
The real problem is the spreadsheet, I've just started a new job and the spreadsheet I've inherited is horrible, 90+ sheets filled with all sorts of crap. However redoing the spreadsheet in a sane fashion isn't really an option at the moment and I need to get on with identifying all the cells with errors and fixing them so does anyone know of a way in which to deal with merged cells when trying to determine the highest column and highest row to contain data?
Bookmarks