Hi all,
I have a function that works out which is the last column containing data on a worksheet, but for some reason it is alwys retuning 0. Just wondering if anybody is able to spot where I am going wrong.
Here is the function I am using -
Public Function LastColumn(Optional WorksheetName As String) As Integer
' Last Column Function
' Function finds the last column in a worksheet with data in it
If WorksheetName = vbNullString Then WorksheetName = ActiveSheet.Name
With Worksheets(WorksheetName)
On Error Resume Next
LastColumn = .Cells.Find("*", .Cells(1), xlFormulas, xlWhole, xlByColumns, xlPrevious).Column
If Err <> 0 Then LastColumn = 0
End With
End Function
And here are the ways I have tried retrieving data from the function, all of which return 0 -
iColumn = LastColumn(wS22.Name)
MsgBox iColumn
MsgBox LastColumn(wS22.Name)
Dim SheetToCheck As String
SheetToCheck = wS22.Name
iColumn = LastColumn(SheetToCheck)
MsgBox iColumn
When I take the Error handleing out I am getting the error 'Object Variable or With Block Variable not set'. All variables are correctly declered.
Any help is greatly appriciated.
Thanks.
Bookmarks