VBA Script to use any open file when copying and pasting

G

Guest

I have a VBA script that copys data from a particular excel sheet into a new
workbook.

Currently, if the file name changes, the script does not work. For the code
below, what do I need to enter that ANY file name will work.

Workbooks.Add
Windows("SpecificFile.xls").Activate
Sheets("Assumptions").Select

What would replace Windows("SpecificFile.xls").Activate such that if a file
opened called File1.xls, or File9.xls, etc, woudl work.

Charles
 
G

Guest

Dim myFileName As String
Dim myWorksheet As String
'assume you want to return to current workbook after
' creating new workbook
myFileName = Application.ActiveWorkbook.Name
'assume you want to return to current worksheet after
' creating new workbook
myWorksheet = Application.ActiveSheet.Name

Workbooks.Add

Windows(myFileName).Activate
Sheets(myWorksheet).Select

--
HTH,
Gary Brown
(e-mail address removed)
If this post was helpful to you, please select ''YES'' at the bottom of the
post.
 
G

Guest

Thank you for the quick resopnse.

Gary, I am running into an error when I enter the code. What I should have
mentioned is that I have 4 tabs on in the .xls file itself, with each tab
being selected at one point in the script to be copied over to the new
workbook.

Please advise.

Charles
 
G

Guest

What's the error? And what is the code that's causing it?
--
HTH,
Gary Brown
(e-mail address removed)
If this post was helpful to you, please select ''YES'' at the bottom of the
post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top