4

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
2
  • Are you having the add-in run on workbook open? Commented Jul 17, 2013 at 5:20
  • 7
    There might not be an ActiveWorkbook if your add-in is the only workbook loaded... Commented Jul 17, 2013 at 5:33

3 Answers 3

9

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

Sign up to request clarification or add additional context in comments.

Comments

1

Check if the workbook in Excel is asking you if you want to open a write-protected version or not. I think while this question is present the workbook is not considered Active, nor is any other

Comments

0

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

Comments

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.