0

I have created an add-in using VBA which is a workbook with calculations. The add-in has a userform to extract relevant information from access database and populates the workbook. After the data is populated, calculations are performed in Sheet1. I need to paste the worksheet "Sheet1" from the add-in worksheet to a new workbook on running the add-in macro.

When I run the add-in however, the worksheet appears to be hidden so my data is not updating. I get this error: " Run-time error '1004': Method 'Worksheets' of object '_Global' failed".

Can someone tell me how to work with an add-in which has a worksheet where the required calculations are performed?

The intriguing part is when I load the add-in after removing it from the list of add-ins in excel, it runs perfectly. But when I re-run the macro, the worksheet becomes hidden, so the same error appears. I am fairly new to VBA so any suggestions would be appreciated!

Edit

Code:

Private Sub OptionOK_Click() 'On selecting OK from userform
  Dim ws1 As Worksheet
  Sheets("Sheet1").Visible = True 
  Set ws1 = Worksheets("Sheet1") 

 'User Form Validation 
  If Trim(Me.cboData.value) = "" Then 
    Me.cboData.SetFocus 
    MsgBox "Please complete the form" 
    Exit Sub 
  End If 

 'copies data to given cell in excel     
  ws1.Range("A1").value = Me.cboData.value 

 'To copy selection from "Sheet1" into new workbook 
Workbooks("myaddin.xlam").Sheets(1).Copy 
End Sub 

I get the error on ...> Sheets("Sheet1").Visible = True.

4

2 回答 2

3

我刚刚意识到我必须在加载项 VBA 代码中使用“ThisWorkbook”。

Set ws1 = ThisWorkbook.Sheets ("Sheet1")

工作簿中的 VBA 代码应使用“ThisWorkbook”来引用加载项内的工作表或范围。

于 2013-10-02T13:25:57.577 回答
0

如果您知道它是什么工作表并且您可以访问加载项代码,只需确保它在引发错误的行之前可见。

Sheets("Sheet3").Visible = True

我怀疑您还有另一个问题,因为您仍然可以在代码中引用隐藏的工作表。


你确定这条线是正确的:

Workbooks("myaddin.xlam").Sheets(1).Copy

在您引用工作表名称之前,您现在引用工作簿中工作表的位置。

于 2013-10-01T20:56:30.780 回答