copy data to new workbook

gssachin

Board Regular
Joined
Nov 14, 2013
Messages
155
Dear All,

I am trying to open a new workbook and then copy date from main sheet to newly open workbook but getting error at
"Workbooks(wname).Activate" as "subscript out of range"

how to solve this.... I used workbook (2) instead of wname and its work but following macro not working..



Sub nworkbook()
Dim wname As String


wname = InputBox("Type here New File Name")


Workbooks.Add
ActiveWorkbook.SaveAs "C:\Users\Downloads\Excel_Training" & wname


ThisWorkbook.Activate


Sheet1.Activate


'Range("a1", Range("a1").End(xlDown).End(xlToRight)).Copy
Range("a1").CurrentRegion.Copy


Workbooks(wname).Activate


Range("a1").PasteSpecial (xlPasteColumnWidths)
Range("a1").PasteSpecial (xlPasteAll)


Workbooks(wname).Save
Workbooks(wname).Close
End Sub
 
Last edited:

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
You are missing a \ from the end of the file path
 
Upvote 0
Ooops, my mistake...

But in original code it was there (i.e ""), My Codes are working fine, they ask user to provide file name, then it will save to given location, next they going to original workbook and copy the given range and getting error while activating newly created workbook.

I m trying to use Workbooks(wname).Activate is this wname is user given name (i.e. filename) but getting error "subscript out of range" instead of "wname" if I use "2" the its working and macro is pasting date in new workbook but not as I required (i.e Workbooks(wname).Activate)
 
Last edited:
Upvote 0
What is the value of wname?
Does it include the file extension?
 
Upvote 0
No user will only provide name he will not enter file extension. if user given the its working fine.
How to incorporate file extension in Workbooks(wname).Activate
 
Upvote 0
hey,

I tried this Workbooks(wname & ".xlsx").Activate and its work for me. Thanks for helping :)
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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