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!
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!