Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBA - Loading (Name) into an Array

Posted on 2011-02-24
11
Medium Priority
?
767 Views
Last Modified: 2012-11-06
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
0
Comment
Question by:redrumkev
  • 4
  • 4
  • 3
11 Comments
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34974780
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 http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Word/Q_26763023.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

0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 2000 total points
ID: 34974788
use:

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

Kevin
0
 
LVL 81

Assisted Solution

by:zorvek (Kevin Jones)
zorvek (Kevin Jones) earned 2000 total points
ID: 34974798
?You can't pass a string variable for the Array selection

You can in the Northern Hemisphere. Too many poisonous snakes down there.

Kevin
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Author Comment

by:redrumkev
ID: 34974844
Dave,

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

(Name) versus Name Property
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
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34974868
> ?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
0
 
LVL 23

Author Comment

by:redrumkev
ID: 34974880
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

0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 34974924
:)

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
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 34974944
>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
0
 
LVL 23

Author Comment

by:redrumkev
ID: 34974956
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!
0
 
LVL 23

Author Closing Comment

by:redrumkev
ID: 34974977
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
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 38574510
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
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing numerous questions for Dynamic Data Validation I notice that most have used Visual Basic to solve the problem. This suggestion is purely formula based and can be used in multiple rows.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question