using variable with .Activate

S

Super Slueth

Hi

I'm trying to use a variable in the following code but i get an error.

Subscript out of range


I am runing a For/Next loop and need to change the file name for every
loop


Dim MyString AS String
Dim MyFileName AS String


MyString = C:\path\filename
MyFileName = somefilename



Windows(MyString).Activate

Sheets(MyFileNAme).Select


Any suggestion on how to do this correctly.


Many thanks
 
G

Gary''s Student

Enclose the string in double-quotes:

MyString = "C:\path\filename"

The Sheets().Activate selects a tab not a workbook
 
B

Bob Phillips

Use the filename only, not path and filename.

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
T

Tom Ogilvy

Dim MyString AS String
Dim MySheetName AS String


MyString = "filename.xls" ' no path
MySheetName = "sheet1" ' as an example



Windows(MyString).Activate

Sheets(MyFileNAme).Select
 

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

Top