Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

assigning variable workbook usin

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Assigning a variable to a workbook using "Set"

Assigning a variable to a workbook using "Set"

ResolvedVersion 2007

Roger has attended:
Excel VBA Intro Intermediate course
Excel VBA Advanced course

Assigning a variable to a workbook using "Set"

Can someone tell me what is wrong with the second statement below, please? The first one works OK and I am certain that the path is correct in the second.

Thanks

Roger

Set wb = Workbooks("SetVar.xlsm")
Set wb = Workbooks("C:\msoffice\excel\SetVar.xlsm")

Edited on Mon 27 Feb 2012, 12:10

RE: Assigning a variable to a workbook using "Set"

Hi Roger.

The Workbooks collection refers to the open workbooks so it doesn't include an option for the file path.

Could the path "C:\moffice\excel\" be added later as a variable? For example for saving the file:

Dim fp as String
fp = "C:\moffice\exel\"
Set wb = Workbooks("SetVar.xlsm")
wb.SaveAs Filename:=fp & "Setvar.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

Regards
Doug
Best STL

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Naming and Using Constants

Constants make calculations easier so worksheets are more easily understood. Constant values also need to be given relevant and memorably names. It is also easier to change the value of a constant.

For example:
Instead of entering 17.5% in each cell when you generate a VAT amount you could name a Constant "VAT" and assigning a "0.175" value to it. To do this:

From the 'Insert' menu select 'Name', then select 'Define'.

Enter the constant

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.1 secs.