Solved

VBA - Loading (Name) into an Array

Posted on 2011-02-24
11
714 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 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now