[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

variable substitution  in a VBA statement

Posted on 2009-12-28
6
Medium Priority
?
332 Views
Last Modified: 2012-05-08
I had this statement that works fine:
 ActiveSheet.Copy after:=Workbooks("Tables.xlsx").Sheets(Workbooks("Tables.xlsx").Sheets.Count)

But now I need to use a variable string for the filename (called SaveFn)

this doesn't work
 ActiveSheet.Copy after:="Workbooks(" & SaveFn & ").Sheets(Workbooks(" & SaveFn & ").Sheets.Count)"

what is the proper way to do this?
 
0
Comment
Question by:derekackerman
  • 4
  • 2
6 Comments
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 26131987
ActiveSheet.Copy after:=Workbooks(SaveFn).Sheets(Workbooks(SaveFn).Sheets.Count)

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 26131992
You use variables the same way you use strings and other constants.

Kevin
0
 

Author Comment

by:derekackerman
ID: 26132799

SaveFn is a variable

    SaveFn = "Table" & yy & ".xlsx"
    Set NewBook = Workbooks.Add
    With NewBook
        .Title = "CPU Data" & yyyy
        .SaveAs Filename:=SaveFn
    End With

later on in the process, this gets an error:

 ActiveSheet.Copy after:=Workbooks(SaveFn).Sheets(Workbooks(SaveFn).Sheets.Count)

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 26132953
What is the error?

Another way to do this is to keep the variable NewBook and use it:

ActiveSheet.Copy after:=NewBook.Sheets(NewBook.Sheets.Count)

Kevin
0
 

Author Comment

by:derekackerman
ID: 26132990
are you reading my notes? "SaveFn" is a VARIABLE - NOT a filename, the statement goes to the error handler, have you tested any of the code you sent?
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 26133050
Have you read my posts? I stated very clearly:

"You use variables the same way you use strings and other constants."

So this statement:

ActiveSheet.Copy after:=Workbooks(SaveFn).Sheets(Workbooks(SaveFn).Sheets.Count)

is the same as:

ActiveSheet.Copy after:=Workbooks("Tables.xlsx").Sheets(Workbooks("Tables.xlsx").Sheets.Count)

Assuming, of course, SaveFn = "Tables.xlsx".

Did you even try my code? I provided this code:

ActiveSheet.Copy after:=NewBook.Sheets(NewBook.Sheets.Count)

With this statement, again, being crystal clear:

"Another way to do this is to keep the variable NewBook and use it."

Kevin
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question