So I've built an add-in so I can access some of my favorite macros from the ribbon, and I'm loving it.
However, my add-in has a worksheet that contains a table I use as a dictionary of sorts.
Occasionally I need to amend my dictionary, and since add-in worksheets aren't directly visible, I copy it into the active workbook, a la
Public Sub Button3()
'Open Device Types and Numbers Dictionary
ThisWorkbook.Sheets("SheetCompTypes").Copy After:=ActiveWorkbook.ActiveSheet
ActiveSheet.Name = "CompTypesDictionary"
End Sub
That all works great. However, when I'm done amending the dictionary, I want to copy it BACK into the add-in and save it for posterity. I wrote the following code, but it hangs up at wbSource.Sheets("CompTypesDictionary").Copy Before:=wbTarget.Sheets(1) with a Runtime error 1004 "Copy method of Worksheet class failed".
Public Sub Button4()
'Save and Close Device Types and Numbers Dictionary
Dim flSheetTest As Boolean 'result of test to see if sheet exists
Dim wbSource As Workbook 'data is pulled from this workbook
Dim wbTarget As Workbook 'data is put in this workbook
Set wbSource = ActiveWorkbook
Set wbTarget = ThisWorkbook
flSheetTest = MiscSheetExists(shtName:="CompTypesDictionary", wb:=wbSource)
If flSheetTest = True Then
'Dictionary exists in open workbook
flSheetTest = MiscSheetExists(shtName:="SheetCompTypes", wb:=wbTarget)
If flSheetTest = True Then
'Dictionary also exists in add-in, and must be overwritten
wbTarget.Sheets("SheetCompTypes").Delete
End If
'Error on following line!!
wbSource.Sheets("CompTypesDictionary").Copy Before:=wbTarget.Sheets(1)
wbSource.Sheets("CompTypesDictionary").Delete
wbTarget.Save
Else
'Dictionary is not open
Call MsgBox("Component Types Dictionary is not open. Please open it before attempting to save and close it.", vbExclamation, "Dictionary Not Open")
End If
What am I doing wrong?
Thanks!
Nathaniel
Bookmarks