Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import another spreadsheet into existing workbook Excel 2010 1

Status
Not open for further replies.

dheim70

Programmer
Sep 23, 2011
14
US
Hi All,
I'm a novice with VBA and Excel, I am trying to do something that I would think is simple but I can't get it to work. I have an excel (2010) spreadsheet with multiple sheets in it. I want to have a button that will import another spreadsheet from a different workbook when clicked. (the spreadsheet that needs to be imported is in Excel 2003), and is in the same directory as the main spreadsheet that it will be going to. I want to automate this as it will be a monthly thing and I don't want to have to cut and paste each time (because of accuracy concerns) Here is the code I am using:

Private Sub CommandButton1_Click()
Dim wkbSource As Workbook
Dim wkbDest As Workbook
Dim shtToCopy As Worksheet

Set wkbSource = Workbooks.Open("C:\B Drive\TestSpread\UORG0104PCLDMAY.xls")
Set wkbDest = Workbooks("C:\B Drive\TestSpread\NYCPA-May2.xlsm")
Set shtToCopy = wkbSource.Sheets("UORG0409-PCLD")
shtToCopy.Copy wkbDest.Sheets()
End Sub

I get a Runtime error '9' subscript out of range. Can anyone help or suggest different code to do this? Any help is greatly appreciated!

Tia!
 
hi,

" subscript out of range" usually means that a specified object does not exist.

Check your spelling, for instance, the sheet name.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
And it may not be necessary to do the open & copy, if your use MS Query, assuming that the data you want to import is tabular only, not graphics or formatting. Once a querytable is added to your sheet, you need only REFRESH the querytable to get new data. In other words, no VBA required.

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
actually, you did not specify which sheet to place the copy before...
Code:
shtToCopy.Copy wkbDest.Sheets([highlight]1[/highlight])

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Hi Skip, thanks for the response,I did the debug process and I am getting that run time '9' error on the line:
Set wkbDest = Workbooks("C:\B Drive\TestSpread\NYCPA-May2.xlsm")
I double checked the file spelling and it is ok, I even copied and pasted it and the path is the same as the previous line and that seems to be ok.
Any thoughts?

Thanks again!
 
Code:
Set wkbSource = Workbooks.Open("C:\B Drive\TestSpread\UORG0104PCLDMAY.xls")
Set wkbDest = Workbooks[highlight].Open[/highlight]("C:\B Drive\TestSpread\NYCPA-May2.xlsm")

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top