Excel VBA: Select specific multiple sheets

Dear,

I try to select multiple sheets, but i always receive Run Time error 9:Subscript out of range.

When i want to select 2 or more fixed sheet i use:
ThisWorkbook.Worksheets(Array("<Sheet name 1>", "<Sheet name 2>")).Select

Open in new window

and it works.

But in my current case the sheets may vary.
So i created a cell with all the names of the sheets that should be selected. In my case Cell G28 on sheet 9.
I tried several posibilities but receive always the same run time error.
Below the code i tested last:

Dim aSheetnames As Variant
aSheetnames = Sheet9.Range("G28").Value
ThisWorkbook.Worksheets(Array(Sheetnames1)).Select

Open in new window


How can i resolve this problem?
ManuHuygheAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

if they are separated with a comma, pls try
aSheetnames = Split(Sheet9.Range("G28").Value, ",")
ThisWorkbook.Worksheets(aSheetnames).Select

Open in new window

Regards
1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
[ fanpages ]IT Services ConsultantCommented:
What explicit value is in cell [G28]?

How are you defining the list/collection of worksheets to select?
0
Saurabh Singh TeotiaCommented:
This works for me without any problem..

Dim str As String

str = Range("G28").Value
Sheets(Array(str)).Select

Open in new window


However if you add thisworkbook before then that it will give you an error so you need to select without thisworkbook and the code will work...

Saurabh...
0
ManuHuygheAuthor Commented:
@Rgonzo1971: Your solution works perfect.
@Saurabh: i tried also your sugestion but i still receives the run time error 9

Thanks all for the fast response
0
ManuHuygheAuthor Commented:
Dear all,

I use the solution proposed by Rgonzo1971 for a while now and it worked fine.
Until last week.
My project is still in progress, so i'm still adding more and more sheets (up to 19 sheets for the moment and more to come).
After adding and configurating the last 4 sheets, i tried to select 1 of them to print and i receive "Run Time Error '1004': Select method of sheets Class failed".
The strange thing is that when i select 2 of them there is no problem.

The way a work now  is:
-I write all the names of the sheets that i want to print in different cells on a sheet named (Memory) and use one cell on that sheet to gether all the names as one.
Example of the result: Report NL,Report FR,Report ENG
-Afterwards i use this code to select them:
Dim Sheetnames As Variant
Sheetnames = Split(Sheet9.Range("P28").Value, ",")
ThisWorkbook.Worksheets(Sheetnames).Select

Open in new window


What can cause this Run time error?

Thanks in advanced
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.