ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with macro to copy worksheet--odd behavior (https://www.excelbanter.com/excel-programming/315444-problem-macro-copy-worksheet-odd-behavior.html)

DavidH[_2_]

Problem with macro to copy worksheet--odd behavior
 
Hi all,

I'm coming across strange macro behavior in the macro below. The macro is
supposed to copy a worksheet named "General Documentation" from a file and
add it as the first worksheet in the current file. During my testing,
everything worked fine. But when I created an add-in sheet containing the
macro below and others, I get strange behavior. When I open a new worksheet
and immediately run the macro, I get a "method 'sheets' of object _workbook
failed" error. This error only seems to occur when I open Excel, which
starts with the default "Book1", then immediately run the macro. It doesn't
pop up if I enter data in "Book1" before I run it or if I run it on a
previously-saved workbook.

This is the line that errors:

Workbooks(wbDOCname).Sheets("General Documentation").Copy
Befo=CurrentWb.Sheets(1)


Any thoughts on what's happening here?

Thanks!

==============================================
Sub AddDocumentation()

Dim wbDOC As Workbook
Dim wbDOCpath As String
Dim wbDOCname As String
Dim CurrentWb As Workbook

Set CurrentWb = Nothing
Set CurrentWb = ActiveWorkbook
wbDOCpath = "C:\NRGSCExcelUtil\"
wbDOCname = "Documentation Template v7 10 27 04.xls"


If CurrentWb Is Nothing Then
MsgBox "No workbook to add General Documentation to!", vbinformaiton
Exit Sub
End If

If SheetExists("General Documentation") Then
MsgBox "General Documentation sheet already exists in this
workbook", vbInformation
Exit Sub
End If


If WorkbookIsOpen(wbDOCname) Then
Set wbDOC = Workbooks(wbDOCname)
Else
If FileExists(wbDOCpath & wbDOCname) Then
Set wbDOC = Workbooks.Open(Filename:=wbDOCpath & wbDOCname)
Else
MsgBox "The Documentation Template file was not found",
vbInformation
Exit Sub
End If
End If



'MsgBox "Adding sheet to " & CurrentWb, vbInformation
Workbooks(wbDOCname).Sheets("General Documentation").Copy
Befo=CurrentWb.Sheets(1)
ActiveSheet.Name = "General Documentation"
Workbooks(wbDOCname).Close SaveChanges:=False
End Sub



All times are GMT +1. The time now is 01:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com