Syntaxt for WorkBook variable

johnbongrovey

New Member
Joined
Apr 17, 2013
Messages
8
I am trying to put the variable from a file open routin into the workbook statement:

I get the variable for filename:
workbooks.Open Filename:=FName

'Then I pass it to the subroutine which works fine. I would like to enter the Variable in multiple copy commands:
I am just lacking on the actual syntax of the command below

workbooks("Variable here (FName)").ActiveSheet.Range("C2:L2000").Copy

Any help would be appreciated
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
Sorry - don't quite understand what you're saying... almost looks like you just want to store the Workbook as a variable?

Dim WB1 as Workbook
Set WB1 = Workbooks.Open(Filename:=FName)

WB1.Activesheet.range.. (etc).
 

johnbongrovey

New Member
Joined
Apr 17, 2013
Messages
8
I am trying to use a variable foir filename.
Basically I open a file and pull data from it and paste to 2nd file/worksheet. At this time I use a physical filename for both but I want ("filetest.xlsm") to be a variable, see below:
workbooks("filetest.xlsm").ActiveSheet.Range("C2:L2000").Copy
workbooks("FR Gathering.xlsm").Worksheets("sheet1").Range("C2:L2000").PasteSpecial xlPasteValues
 

ClimoC

Well-known Member
Joined
Aug 21, 2009
Messages
584
I am trying to use a variable foir filename.
Basically I open a file and pull data from it and paste to 2nd file/worksheet. At this time I use a physical filename for both but I want ("filetest.xlsm") to be a variable, see below:
workbooks("filetest.xlsm").ActiveSheet.Range("C2:L2000").Copy
workbooks("FR Gathering.xlsm").Worksheets("sheet1").Range("C2:L2000").PasteSpecial xlPasteValues


Yeah - so do exactly what I said...

You can store the Workbook.Name property as a String and then

Workbooks(MyWBNameString).activesheet...

But in general, that'd be bad practice, because it has to evaluate what workbook that is each time. Also if any saving/name changing occured, it won't recognise it. Setting the Workbook Object to a variable is not only more commonplace, but it's safer
 

Watch MrExcel Video

Forum statistics

Threads
1,123,304
Messages
5,600,864
Members
414,409
Latest member
FloordAlex

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
Top