Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created an Add-In for Excel which determines the name of ActiveSheet and ActiveWorkbook. The code I used is below. When I run the Add-In it is showing the above mentioned error after the message box "variables set". But when I run it in macros it is working fine. I don't understand what is happening with the Add-In. Could anyone help me with this?

Sub sheetvalues()
    Dim bk As Workbook, sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
    Dim book As String, sht As String, i As Integer, j As Integer
    Dim att(1 To 4) As String, att_col(1 To 4) As Integer

    MsgBox ("variables set")

    book = ActiveWorkbook.Name
    sht = ActiveSheet.Name
    MsgBox ("names set")

    Set bk = Workbooks.Add
    With bk
        .Title = "MissingValues"
        .SaveAs Filename:="MissingValues.xls"
    End With

    Set sht1 = bk.Sheets.Add
    sht1.Name = "EndOne"
    Set sht2 = bk.Sheets.Add
    sht2.Name = "EndTwo"
    Set sht3 = bk.Sheets.Add
    sht3.Name = "EndThree"

    MsgBox (book & "  " & sht)
    MsgBox ("completed")
End Sub
share|improve this question
    
Are you having the add-in run on workbook open? –  Ripster Jul 17 '13 at 5:20
7  
There might not be an ActiveWorkbook if your add-in is the only workbook loaded... –  Tim Williams Jul 17 '13 at 5:33

2 Answers 2

Like @TimWilliams said, you will get this error if your add-in is the only workbook loaded. In that case there is no active workbook and your code is failing on the line

book = ActiveWorkbook.Name

You can check for the existence of a workbook by adding the following lines:

Set bk = Application.ActiveWorkbook
If bk Is Nothing Then
    MsgBox ("No workbook open. Creating a new one.")
    Set bk = Workbooks.Add(xlWBATWorksheet)
    Set sht1 = bk.ActiveSheet
End If

So you end up with:

Sub sheetvalues()
    Dim bk As Workbook, sht1 As Worksheet, sht2 As Worksheet, sht3 As Worksheet
    Dim book As String, sht As String, i As Integer, j As Integer
    Dim att(1 To 4) As String, att_col(1 To 4) As Integer

    MsgBox ("variables set")

    Set bk = Application.ActiveWorkbook
    If bk Is Nothing Then
        MsgBox ("No workbook open. Creating a new one.")
        Set bk = Workbooks.Add(xlWBATWorksheet)
        Set sht1 = bk.ActiveSheet
    End If


    book = ActiveWorkbook.Name
    sht = ActiveSheet.Name
    MsgBox ("names set")

    Set bk = Workbooks.Add
    With bk
        .Title = "MissingValues"
        .SaveAs Filename:="MissingValues.xls"
    End With

    Set sht1 = bk.Sheets.Add
    sht1.Name = "EndOne"
    Set sht2 = bk.Sheets.Add
    sht2.Name = "EndTwo"
    Set sht3 = bk.Sheets.Add
    sht3.Name = "EndThree"

    MsgBox (book & "  " & sht)
    MsgBox ("completed")
End Sub
share|improve this answer

A common issue that causes this issue is forgetting to use 'Set' with assigning a value to a variable.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.