Not sure what to name this subject

  • Thread starter Thread starter MARTY
  • Start date Start date
M

MARTY

Hello:

If N is an integer, is the following syntax (&N) allowed
in workbook names, objects, etc?

Example: If I write this:

Set MYSHEET&N = Workbooks(Group&N.xls).Sheets
("SheetName").

and then I set N = 1, will VBA interpret this as:

Set MYSHEET1 = Workbooks(Group1.xls).Sheets("SheetName")

? If not (i.e., it doesn't like that), does anyone know
the correct syntax for something like this? Would I have
to use "..." anywhere in order for it to be correct?

Thanks,
MARTY
 
You should set the value for N before using it in a statement
N=1
Set MYSHEET&N = Workbooks("Group "&N".xls").Sheets("SheetName").

Will interpret as: MYSHEET1 = Workbooks(Group1.xls).Sheets("SheetName")
Notice the space between Group and 1 and using N outside of quotes.
Mike F
 
I got a little sloppy
Set MYSHEET&N = Workbooks("Group "&N&".xls").Sheets("SheetName").

Another ampersand will follow a variable if it is in the middle of a name.
Mike F
 
Thank you Mike.
-----Original Message-----
I got a little sloppy
Set MYSHEET&N = Workbooks("Group "&N&".xls").Sheets ("SheetName").

Another ampersand will follow a variable if it is in the middle of a name.
Mike F




.
 
Marty,

You need

Set MYSHEET&N = Workbooks("Group" & N & ".xls").Sheets("SheetName")

N is a VBA variable, the book name is a string, so you need to concatenate
the values into your string.


--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 

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

Back
Top