0

I have the following code:

Last_Column = 0
On Error Resume Next
Last_Column = Sheets("Combined").Cells.Find("", [a1],, , _
                           xlByColumns, xlPrevious).Column

When the sheet has no data it returns Run time error '91': Object variable or With block variable not set,

How can I get it to continue, or what do I have to do?

2 Answers 2

2

[update: and you should be looking for "*" not '""'

You are better off using a range object and then testing whether it exists, ie

Dim rng1 As Range
Dim Last_Column As Long
Set rng1 = Sheets("Combined").Cells.Find("*", [a1], , , xlByColumns, xlPrevious)
If Not rng1 Is Nothing Then
    Last_Column = rng1.Column
Else
    MsgBox "No data", vbCritical
End If
Sign up to request clarification or add additional context in comments.

1 Comment

I just saw your other response. You can continue with formatting either after the Else (different formatting depending on whether Last_Column' exists) or End If` above (ie same formatting for sheet with content or not). My query would be though what would you format in an empty sheet?
0

You should probably check to make sure that the data sheet is not empty or null before running your code. Then inform the user that the sheet is empty.

1 Comment

Hi, but when it's empty I still want to be able to with formating the sheet, eg. headings etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.