Solved

VBA - Loading (Name) into an Array

Posted on 2011-02-24
11
704 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

12 Experts available now in Live!

Get 1:1 Help Now