Solved

VBA - Loading (Name) into an Array

Posted on 2011-02-24
11
745 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

728 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