beginner with macro question...can't copy data from one workbook into another with macros

kcphila

New Member
Joined
Jun 5, 2015
Messages
4
I’m trying to build a macro that copies a selection of columns out of a workbook, opens a new workbook, pastes the selection into that workbook and then saves it. I built the macro, and it ran fine. Then I tried to recreate it in a different workbook, and it doesn’t. It gives the error “Subject out of range” and highlights “ Windows("Book").Activate “ as the problem in VBA. I’m assuming what it’s saying is that it can’t create a new workbook. I can’t understand how this could work once and then not work again, my only theory is that it has to do something with the naming of the workbook I’m creating. I’ve also tried to create a work around by creating a separate workbook then creating a macro to open that workbook and paste my content into it. The problem here is that it gives me an error about how I can’t use the paste function multiple times in a macro.

Help!

Thanks.


Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+a
'
Range("B:B,C:C").Select
Range("C1").Activate
Selection.Copy
Workbooks.Add
Windows("2014Workbook.xlsx").Activate
Windows("Book1").Activate
ActiveSheet.Paste
Application.Left = 204.25
Application.Top = 78.25
Application.CutCopyMode = False
ChDir "C:\Users\Kevin Cox\New Query Small Orders\macros"
ActiveWorkbook.SaveAs Filename:= _
"C:\Users\Kevin Cox\New Query Small Orders\macros\Book1.xlsx", FileFormat:= _
xlOpenXMLWorkbook, CreateBackup:=False
Application.Left = -228.5
Application.Top = 76.75
ActiveWindow.Close
End Sub
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hi Kevin

Use below. Reason you are getting an error is because the workbook you are adding might not be called Book1 and therefore when it's trying to activate that window it cannot find it. Below is not the best way to do it but it gets the job done.

Sub Macro1()
Set wrk = ActiveWorkbook
Set newwrk = Workbooks.Add
wrk.Activate
Range("B:B,C:C").Copy
newwrk.Activate
ActiveSheet.Paste
newwrk.SaveAs Filename:= "C:\Users\Kevin Cox\New Query Small Orders\macros\Book1.xlsx"
newwrk.close
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,207,091
Messages
6,076,523
Members
446,212
Latest member
KJAYPAL200

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