Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default Select sheets based on what is in a range.

Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Select sheets based on what is in a range.

Hi Steve

You can make a array of the the cell values
There is no error check in this example

Sub aa()
Dim arr() As String
Dim N As Integer

N = 0
For Each cell In Columns("A").SpecialCells(xlCellTypeConstants)
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Value
Next

ActiveWorkbook.Worksheets(arr).Select

End Sub

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Steve" wrote in message ...
Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default Select sheets based on what is in a range.

Ron, how could I build in an error check for this example?

"Ron de Bruin" wrote:

Hi Steve

You can make a array of the the cell values
There is no error check in this example

Sub aa()
Dim arr() As String
Dim N As Integer

N = 0
For Each cell In Columns("A").SpecialCells(xlCellTypeConstants)
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Value
Next

ActiveWorkbook.Worksheets(arr).Select

End Sub

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Steve" wrote in message ...
Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,123
Default Select sheets based on what is in a range.

Hi Steve

First SpecialCells gives a error if there are no xlCellTypeConstants and also
you must test if the sheet names exist

Try this together with the function in a normal module

Sub Test()
Dim arr() As String
Dim N As Integer

N = 0
On Error Resume Next
For Each cell In Columns("A").SpecialCells(xlCellTypeConstants)
If SheetExists(cell.Value) = True Then
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Value
End If
Next

If N 0 Then
ActiveWorkbook.Worksheets(arr).Select
End If

End Sub

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function



--
Regards Ron De Bruin
http://www.rondebruin.nl



"Steve" wrote in message ...
Ron, how could I build in an error check for this example?

"Ron de Bruin" wrote:

Hi Steve

You can make a array of the the cell values
There is no error check in this example

Sub aa()
Dim arr() As String
Dim N As Integer

N = 0
For Each cell In Columns("A").SpecialCells(xlCellTypeConstants)
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Value
Next

ActiveWorkbook.Worksheets(arr).Select

End Sub

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Steve" wrote in message ...
Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?






  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default Select sheets based on what is in a range.

Ron, thank you very much, that worked perfectly. Have a good one.

Steve

"Ron de Bruin" wrote:

Hi Steve

First SpecialCells gives a error if there are no xlCellTypeConstants and also
you must test if the sheet names exist

Try this together with the function in a normal module

Sub Test()
Dim arr() As String
Dim N As Integer

N = 0
On Error Resume Next
For Each cell In Columns("A").SpecialCells(xlCellTypeConstants)
If SheetExists(cell.Value) = True Then
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Value
End If
Next

If N 0 Then
ActiveWorkbook.Worksheets(arr).Select
End If

End Sub

Function SheetExists(SName As String, _
Optional ByVal WB As Workbook) As Boolean
'Chip Pearson
On Error Resume Next
If WB Is Nothing Then Set WB = ThisWorkbook
SheetExists = CBool(Len(WB.Sheets(SName).Name))
End Function



--
Regards Ron De Bruin
http://www.rondebruin.nl



"Steve" wrote in message ...
Ron, how could I build in an error check for this example?

"Ron de Bruin" wrote:

Hi Steve

You can make a array of the the cell values
There is no error check in this example

Sub aa()
Dim arr() As String
Dim N As Integer

N = 0
For Each cell In Columns("A").SpecialCells(xlCellTypeConstants)
N = N + 1
ReDim Preserve arr(1 To N)
arr(N) = cell.Value
Next

ActiveWorkbook.Worksheets(arr).Select

End Sub

--
Regards Ron De Bruin
http://www.rondebruin.nl



"Steve" wrote in message ...
Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?








  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Select sheets based on what is in a range.

Give this a try...

Sub SelectSheets()
Dim rng As Range
Dim rngTabs As Range

Set rngTabs = Range("A1", Cells(Rows.Count, "A").End(xlUp))
On Error Resume Next
For Each rng In rngTabs
Sheets(rng.Value).Select False
Next rng
On Error GoTo 0

End Sub
--
HTH...

Jim Thomlinson


"Steve" wrote:

Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,814
Default Select sheets based on what is in a range.

Jim, thanks for the response. The macro works except that it always selects
the tab with the info in column A even when that tab name is not listed. Any
way to fix this.

Thanks, Steve

"Jim Thomlinson" wrote:

Give this a try...

Sub SelectSheets()
Dim rng As Range
Dim rngTabs As Range

Set rngTabs = Range("A1", Cells(Rows.Count, "A").End(xlUp))
On Error Resume Next
For Each rng In rngTabs
Sheets(rng.Value).Select False
Next rng
On Error GoTo 0

End Sub
--
HTH...

Jim Thomlinson


"Steve" wrote:

Let say I have 5 tabs labeled AAA, BBB, CCC, DDD and EEE. In column A
starting in cell A1, I can have the name of 2, 3 or 4 tabs names listed
(number of tabs to select varies). Is there any way to program a macro to
look at the tab names listed in column A and select just those tabs?

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Select sheets ina workbook based on radio buttons and move them Larry Fitch Excel Worksheet Functions 4 September 28th 09 01:42 AM
CODE 2 SELECT SHEETS LISTED IN A RANGE Faraz A. Qureshi Excel Discussion (Misc queries) 1 July 30th 09 07:31 AM
Using VBA select sll sheets based on Criteria on each sheet. AirgasRob Excel Discussion (Misc queries) 4 September 3rd 08 03:11 PM
How to Select Range based on dates in cells Mike[_92_] Excel Programming 1 December 30th 04 03:53 AM
Select Range of sheets Carol[_3_] Excel Programming 6 April 29th 04 02:09 PM


All times are GMT +1. The time now is 12:21 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"