Link to home
Start Free TrialLog in
Avatar of redrumkev
redrumkevFlag for United States of America

asked on

VBA - Loading (Name) into an Array

Experts,

Attached is an example with comments in the code, as noted below.

I have the following Sub that works based on the sheet name which is visible to the user, not based on the (Name) seen within the VBE

Sheets(Array("Sheet1", "Sheet2")).Select

Open in new window


I tried using the (Name) that I put in, but I get an error:

Sheets(Array("NAMESOMETHING", "RENAMESHEET2")).Select

Open in new window


I have successfully used the following:

Dim WS As Worksheet
            
     Set WS = NAMESOMETHING
              ' This activates NAMESOMETHING(Sheet1)
         WS.ACTIVATE

Open in new window


But I do not know how to select multiple sheets, using the (Name) at once so that I can then say:

ActiveWindow.SelectedSheets.PrintPreview

Open in new window


Regards,
Kevin

   
Sub ACTIVATE2()

' The following works, but is based on Name, not (NAME)
        
Sheets(Array("Sheet1", "Sheet2")).Select
        
' This does not work, calling (NAME) into the above array
            
Sheets(Array("NAMESOMETHING", "RENAMESHEET2")).Select
        
' This below works, but only one Worksheet at a time.
        
Dim WS As Worksheet
            
Set WS = NAMESOMETHING  
         ' This activates NAMESOMETHING(Sheet1)
    WS.ACTIVATE
            
' I want to Select both "NAMESOMETHING" and "RENAMESHEET2"
' So that I can "Print Preview" the two at once!
        
' Once selected, I would use something such as:
' ActiveWindow.SelectedSheets.PrintPreview

End Sub

Open in new window

ActivateTwoSheetsAtOnce.xls
Avatar of Dave
Dave
Flag of Australia image

You can't pass a string variable for the Array selection

You can do it if you build an actual array of sheets to be seleted

From https://www.experts-exchange.com/questions/26763023/Excel-Problem-with-selecting-multiple-sheets-with-array.html

These sample codes will select every second sheet (as an example) using
1) Array method
2) The Select False argument

Cheers

Dave
Sub ShtSelect()
' credit to http://www.tushar-mehta.com/  
Dim shArray()
    Dim lngsht As Long
    ReDim shArray(0)
    For lngsht = 1 To Sheets.Count Step 2
        shArray(UBound(shArray)) = Sheets(lngsht).Name
        ReDim Preserve shArray(UBound(shArray) + 1)
    Next
    ReDim Preserve shArray(UBound(shArray) - 1)
    Sheets(shArray).Select
End Sub


Sub SelectAllSheets()
'modified from http://vbaexpress.com/kb/getarticle.php?kb_id=197
    Dim ws As Worksheet
    Application.ScreenUpdating = False
    ActiveWorkbook.Sheets(1).Activate
    For Each ws In ActiveWorkbook.Sheets
        If ws.Index Mod 2 = 1 Then ws.Select False
    Next ws
    Application.ScreenUpdating = True
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of redrumkev

ASKER

Dave,

So there is no way to call the (Name) - blue arrow below, instead of the red arrow?

User generated image
I guess I am not following your code above. I am going to have say 7 worksheets where I have changed the (Name) box to something of meaning to me. I want to call these 7 sheets, so this can be hard coded, that whenever the module is executed it ends up selecting these 7 sheets and then displays a print preview of them.

Kevin
> ?You can't pass a string variable for the Array selection

Sounds like I need to recheck my statement when I have time to access Excel - I have tried (and failed) passing the variable previously

But if the snake fearing guy says it can be done, then I'd bank on that

Cheers

Dave
Zorvek: (Quote) "You can in the Northern Hemisphere. Too many poisonous snakes down there."

LOL

The following does select Sheet 1 and 2 (NAMESOMETHING and RENAMESHEET2), but does not show them in the print preview window. What would I have to change on the second line?

Thank you,
Kevin
Sub ACTIVATE2()
 
   Sheets(Array(NAMESOMETHING.Name, RENAMESHEET2.Name)).Select
   ActiveWindow.SelectedSheets.PrintPreview
            
End Sub

Open in new window

:)

The question I'd referred to built up a string array which failed

But yes passing the actual names associaed with the sheet code names works fine

See you next week Kevin, I'm looking forward to catching up

Cheers

Dave
>See you next week Kevin, I'm looking forward to catching up

Most definitely!

vekmurder:

The sheets are there in the print preview...multiple pages ;-)

Kevin
Figured it out... I didn't have anything on the sheets, so blank sheets won't display. I put "asdf" in a cell on each and now they come up!

Thank you!
This is great, I use the (Name) box a lot, so now I can call on it!

Thank you for the quick replies and the solution!

Kevin
As a follow-up to this thread which may be helpful to others passing by I have since realised that a 1D string array will work when passed to
Sheets(x).Select

Open in new window

So to select all sheets from a sheet called "Cover" to the end of a workbook can be done in a single shot (without looping) with code such

Sheets(Application.Transpose(Application.Evaluate("=ROW(" & Sheets("Cover").Index & ":" & Sheets(Sheets.Count).Index & ")"))).Select

Open in new window

This code comes from a solution I added here at stackoverFlow

Regards

Dave