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
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
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).
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,536
Messages
6,114,207
Members
448,554
Latest member
Gleisner2

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