The Enthusiast
The Enthusiast

Reputation: 1

VBA run-time error ‘91’: Object variable or With block variable not set

I'm brand new here and although I'm super handy with Excel I have minimal knowledge when it comes to VBA. I've had an Excel program that we use at work dropped in my lap because it's started throwing multiple errors and I'm the "best" person for the job :(

I've searched this most excellent forum and a couple others and have been able to solve most of my problems, but now a new error is popping up and I cannot figure out why.

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

It pops up when I start the program.

The highlighted area in debugger is:

If ActiveWorkbook.Name = wbkTT Then

I'm posting the section of code below

Thanks a lot for any and all help!

' TTV2.xlam version 5/15/12 14:00

Const wbkTT As String = "Time Tracker ver2.xlsm"
Const RibbonxAddin As String = "TTV2.xlam"


Option Explicit
Private Sub Workbook_Open()

Dim strName As String
Dim strPath As String
Dim strNewShow As String
Dim strDate As String
Dim strTime As String
Dim strDepartment As String
Dim strOS As String
Dim strVersion As String
Dim dtFormat As String
Dim myAddIn As AddIn
Dim wbkDateOrder As Integer
Dim timedropdown As Boolean

strVersion = Application.Version
strOS = Application.OperatingSystem
dtFormat = DateFormat
newTTwbk = wbkTT

If strVersion < 14 Then
    MsgBox ("You are running an outdated version of Excel." & Chr(10) & _
        "Time Tracker will only run on MS Excel 2011 for Mac")
    ActiveWorkbook.Close
Exit Sub
Else
    wbkDateOrder = Application.International(xlDateOrder)
    strTime = Format(time, "hhmm")
    strDate = Format(Date, "mmdd")
    If ActiveWorkbook.Name = wbkTT Then
        strName = Replace(Workbooks(wbkTT).Sheets("Show Info").Range("A2").Value, " ", "")
        ActiveWorkbook.Sheets("Preferences").Range("D2:D3").NumberFormat = dtFormat
        ActiveWorkbook.Sheets("Data").Range("B:B").NumberFormat = dtFormat
    End If '
AddMenu
    ActiveWorkbook.Sheets("Preferences").visible = True
    timedropdown = ActiveWorkbook.Sheets("Preferences").Range("E3")
    If timedropdown Then
        ActiveWorkbook.Sheets("Data").Range("G:L").Validation.Delete
        ActiveWorkbook.Sheets("Data").Range("G:L").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Time"
    Else
        ActiveWorkbook.Sheets("Data").Range("G:L").Validation.Delete
    End If
   If ActiveWorkbook.Sheets("Show Info").Range("A2").Value = "TTV2" Then
        If strOS Like "*Win*" Then
            strPath = ThisWorkbook.Path & "\"
        Else
            strPath = ThisWorkbook.Path & ":"
        End If
        strName = InputBox(Prompt:="Show Name please.", Title:="ENTER SHOW NAME", Default:="Show Name Here")
        strDepartment = InputBox(Prompt:="Enter your Department:", Title:="ENTER DEPARTMENT", Default:="Department Here")
        showcrewform
      If strName = "Show Name Here" Or strName = vbNullString Then
            Exit Sub
        Else
            ActiveWorkbook.Sheets("Show Info").Range("A2").Value = strName
            ActiveWorkbook.Sheets("Show Info").Range("B2").Value = strDepartment
            If Not strOS Like "*Mac*" Then
                strNewShow = strPath & strName & "_" & strDate & "_" & strTime & ".xlsm"
            Else
                strNewShow = strPath & strName & "_" & strDate & "_" & strTime & ".xlsm"
            End If
            ActiveWorkbook.SaveAs strNewShow, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            MsgBox ("You are now working on the file: " & strNewShow)
            loadwbkName
            ResetDataSheet
            Exit Sub
        End If
    End If
    If strOS Like "*Mac*" Then
        AddMenu
    End If
    ResetDataSheet
End If
End Sub

Upvotes: 0

Views: 2483

Answers (1)

B Hart
B Hart

Reputation: 1118

Added some missing declarations and End If statement... I don't get any run-time error with the below. (EDIT: Added new code)

' TTV2.xlam version 5/15/12 14:00

Const wbkTT As String = "Time Tracker ver2.xlsm"
Const RibbonxAddin As String = "TTV2.xlam"

'ADDED DECLARATIONS
Dim dtFormat As String
Dim DateFormat
Dim newTTwbk

Option Explicit

Private Sub Workbook_Open()
Dim strName As String
Dim strPath As String
Dim strNewShow As String
Dim strDate As String
Dim strTime As String
Dim strDepartment As String
Dim strOS As String
Dim strVersion As String
Dim dtFormat As String
Dim myAddIn As AddIn
Dim wbkDateOrder As Integer
Dim timedropdown As Boolean

' ADDED DECLARATIONS
Dim WB As Workbook

'ADDED CODE
Set WB = ActiveWorkbook

strVersion = Application.Version
strOS = Application.OperatingSystem
dtFormat = DateFormat
newTTwbk = wbkTT

If strVersion < 14 Then
    MsgBox ("You are running an outdated version of Excel." & Chr(10) & _
        "Time Tracker will only run on MS Excel 2011 for Mac")
    WB.Close 'SAVE? (TRUE/FALSE)
    Exit Sub
Else
    wbkDateOrder = Application.International(xlDateOrder)
    strTime = Format(Time, "hhmm")
    strDate = Format(Date, "mmdd")
    If WB.Name = wbkTT Then
        strName = Replace(Workbooks(wbkTT).Sheets("Show Info").Range("A2").Value, " ", "")
        WB.Sheets("Preferences").Range("D2:D3").NumberFormat = dtFormat
        WB.Sheets("Data").Range("B:B").NumberFormat = dtFormat
    End If
    'AddMenu
    WB.Sheets("Preferences").Visible = True
    timedropdown = WB.Sheets("Preferences").Range("E3")
    If timedropdown Then
        WB.Sheets("Data").Range("G:L").Validation.Delete
        WB.Sheets("Data").Range("G:L").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="=Time"
    Else
        WB.Sheets("Data").Range("G:L").Validation.Delete
    End If
    If WB.Sheets("Show Info").Range("A2").Value = "TTV2" Then
        If strOS Like "*Win*" Then
            strPath = ThisWorkbook.Path & "\"
        Else
            strPath = ThisWorkbook.Path & ":"
        End If
        strName = InputBox(Prompt:="Show Name please.", Title:="ENTER SHOW NAME", Default:="Show Name Here")
        strDepartment = InputBox(Prompt:="Enter your Department:", Title:="ENTER DEPARTMENT", Default:="Department Here")
        'showcrewform
        If strName = "Show Name Here" Or strName = vbNullString Then
            Exit Sub
        Else
            WB.Sheets("Show Info").Range("A2").Value = strName
            WB.Sheets("Show Info").Range("B2").Value = strDepartment
            If Not strOS Like "*Mac*" Then
                strNewShow = strPath & strName & "_" & strDate & "_" & strTime & ".xlsm"
            Else
                strNewShow = strPath & strName & "_" & strDate & "_" & strTime & ".xlsm"
            End If
            WB.SaveAs strNewShow, FileFormat:=xlOpenXMLWorkbookMacroEnabled
            MsgBox ("You are now working on the file: " & strNewShow)
            'loadwbkName
            'ResetDataSheet
            Exit Sub
        End If
    End If
    If strOS Like "*Mac*" Then
        'AddMenu
    End If
    'ResetDataSheet
End If
End Sub

Upvotes: 1

Related Questions