Ajedi32
Ajedi32

Reputation: 48368

What is run time error 91, and why is it showing up in my Excel VBA script?

Okay, so I'm trying to write a simple script in VBA for Excel that changes an AutoFilter based on some value the user selects in a cell on the spreadsheet. So far it's been working out pretty well, but now I'm getting the following error and I can't figure out what's causing it:

Run-time error '91': Object variable or With block variable not set

Keep in mind that this is literally the first time I've ever tried to write anything in VBA so I'm not very familiar with the language. I am very familiar with Excel though and I know several other programming languages (Java, JavaScript, Ruby, LUA).

Here's the code I wrote; the error is happening on line 9.

Private Sub Worksheet_Change(ByVal Target As Range)

    '' Review Level Changed ''
    If Target.Address = Worksheets("Sheet1").Range("review_level").Address Then

        ' MsgBox "You just changed " & Target.Address & " to " & Target.Value ' Debug
        Dim oldProtection As Protection
        If Worksheets("Sheet1").ProtectContents = True Then
            oldProtection = Worksheets("Sheet1").Protection ' It errors out here
            Worksheets("Sheet1").Unprotect
        End If

        If Target = "B" Then
            ActiveSheet.ListObjects("review_checklist").Range.AutoFilter Field:=2, _
                Criteria1:=Array("B", "C", "D"), Operator:=xlFilterValues
        ElseIf Target = "C" Then
            ActiveSheet.ListObjects("review_checklist").Range.AutoFilter Field:=2, _
                Criteria1:="=C", Operator:=xlOr, Criteria2:="=D"
        ElseIf Target = "D" Then
            ActiveSheet.ListObjects("review_checklist").Range.AutoFilter Field:=2, _
                Criteria1:="=D"
        End If

        If Not IsEmpty(oldProtection) Then ' Btw, this IS how you check that oldProtection isn't null, right?
            Call ProtectWithProtection(oldProtection)
        End If

    End If

End Sub

Private Sub ProtectWithProtection(ByRef Protect As Protection)
    Worksheets("Sheet1").Protect ' ToDo: Use attributes from Protect
End Sub

This code is located inside "Sheet1" in my Excel project. Note that I am running Excel 2007.

Upvotes: 2

Views: 25282

Answers (1)

Kevin Pope
Kevin Pope

Reputation: 2982

Whenever you have an object in VBA, you need to assign it a value using the Set operator. For example:

Set oldProtection = Worksheets("Sheet1").Protection

Upvotes: 5

Related Questions