JesusMonroe
JesusMonroe

Reputation: 1601

VBA - Can't use Sheets object without error

I have an Excel file with one sheet called Master.

I have a button to delete a value from the database. This is what I have written that's relevant:

Sub delete_this()
    On Error GoTo ErrorCatch
    simple = Sheets("Master")
    .........
ErrorCatch
    MsgBox(Err.Description)
End Sub

It fails immediately when I use Sheets, saying "Application-defined or object-defined error." However I'm using other code that I know works as a reference and they called this no problem (though their file had multiple sheets).

Also in general I'm new to VBA and find it pretty unintuitive with its error messages, and finding out variable values. So any advice there would also be appreciated.

Upvotes: 0

Views: 303

Answers (1)

basic_one
basic_one

Reputation: 245

As mentioned in a comment by Scott Craner, you need to use the Set statement whenever assigning an object to a variable when using VBA. This little gotcha doesn't exist in VB.NET, and is easy to forget about these days.

Set simple = Sheets("Master")

Upvotes: 3

Related Questions