Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default 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

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strange Copy/Paste behavior Ray Excel Discussion (Misc queries) 3 July 9th 07 03:27 PM
Strange copy & paste behavior Doug Kanter Excel Discussion (Misc queries) 1 April 20th 06 12:35 AM
worksheet copy problem dan Excel Programming 1 January 24th 04 02:26 AM
worksheet copy problem jim apostolidis Excel Programming 1 October 16th 03 01:38 PM


All times are GMT +1. The time now is 03:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"