Copying to another workbook error

vthokienj

Board Regular
Joined
Aug 1, 2011
Messages
105
Office Version
  1. 365
Platform
  1. Windows
I am at a complete loss as to why this code does not work. It gives the 'run time error 9 error'. The other workbook exists and is open after the first block of code. Any help greatly appreciated. Thanks.

Code:
    ' create a new workbook
    Dim newWorkbook As Workbook
    Set newWorkbook = Workbooks.Add
    With newWorkbook
        .Title = "testoutput"
        .Subject = "output"
        .SaveAs filename:="C:\temp\aTestOutput.xlsx"
    End With
Code:
   ' write value to new workbook
   Workbooks("C:\temp\aTestOutput.xlsx").Sheets("Sheet1").Cells(1, 1) = "output"
    'Workbooks("C:\temp\aTestOutput.xlsx").Worksheets(1).Cells(1, 1) = "output"
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
When you refer to an open workbook by name do not use the full path, just the name:

Code:
Workbooks("C:\temp\aTestOutput.xlsx").Sheets("Sheet1").Cells(1, 1) = "output"

Amend to:
Code:
Workbooks("aTestOutput.xlsx").Sheets("Sheet1").Cells(1, 1) = "output"
 
Upvote 0
Greetings vthokienj,

In addition to xenou's comments, I would add that as you have set a good reference to the created workbook, the reference is easily usable, whether your second snippet is in the same procedure or another.

(I am using Excel 2000 at the moment, and not sure about how you are accessing builtinproperties.)

Rich (BB code):
Sub example_1()
Dim wbNew As Workbook
    
    Set wbNew = Workbooks.Add(xlWBATWorksheet)
    With wbNew
        .BuiltinDocumentProperties(1) = "test output"
        .BuiltinDocumentProperties(2) = "output"
        .SaveAs Filename:=ThisWorkbook.Path & "\TestOutput.xls", FileFormat:=xlWorkbookNormal
        .Worksheets(1).Cells(1).Value = "Test Value"
    End With
End Sub
Sub example_2()
Dim wbNew As Workbook
    
    Set wbNew = Workbooks.Add(xlWBATWorksheet)
    With wbNew
        .BuiltinDocumentProperties(1) = "test output"
        .BuiltinDocumentProperties(2) = "output"
        .SaveAs Filename:=ThisWorkbook.Path & "\TestOutput.xls", FileFormat:=xlWorkbookNormal
    End With
    
    Call TryMe(wbNew)
    
    If Not wbNew.Saved Then wbNew.Save
    wbNew.Close False
End Sub
Sub TryMe(wb As Workbook)
    wb.Worksheets(1).Cells(1).Value = "Test Value"
End Sub

Hope that helps,

Mark
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top