ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Check Worksheet Name and Select if Meets Criteria (https://www.excelbanter.com/excel-programming/432237-check-worksheet-name-select-if-meets-criteria.html)

ainbinder

Check Worksheet Name and Select if Meets Criteria
 
Hi everyone, i have what i think is a very easy question. I'm trying
to select all the worksheets in my workbook that start with the
"Data". i wrote this vba code, and even though i put a watch on the
left formula and it shows "Data" in the watch window, it never selects
the sheet. Any help would be much appreciated!!!!

Sub SelectCockpit()
'
' Selects all tabs with cockpit in first 6 digits

Dim wksSheet As Worksheet

Sheets(1).Activate
For Each wksSheet In Worksheets
If Left(wksSheet.Name, 4) = "Data" Then
wksSheet.Select
End If
Next wksSheet

smartin

Check Worksheet Name and Select if Meets Criteria
 
ainbinder wrote:
Hi everyone, i have what i think is a very easy question. I'm trying
to select all the worksheets in my workbook that start with the
"Data". i wrote this vba code, and even though i put a watch on the
left formula and it shows "Data" in the watch window, it never selects
the sheet. Any help would be much appreciated!!!!

Sub SelectCockpit()
'
' Selects all tabs with cockpit in first 6 digits

Dim wksSheet As Worksheet

Sheets(1).Activate
For Each wksSheet In Worksheets
If Left(wksSheet.Name, 4) = "Data" Then
wksSheet.Select
End If
Next wksSheet



I learned something. It turns out selecting multiple worksheets is done
by passing an array to the Select method. Try this:

Sub SelectCockpit()
'
' Selects all tabs with cockpit in first 6 digits

Dim wksSheet As Worksheet
Dim SheetNames() As String
Dim i As Long

i = 1
Sheets(1).Activate
For Each wksSheet In Worksheets
If Left(wksSheet.Name, 4) = "Data" Then
ReDim Preserve SheetNames(1 To i)
SheetNames(i) = wksSheet.Name
i = i + 1
End If
Next wksSheet
Sheets(SheetNames).Select

End Sub


All times are GMT +1. The time now is 05:24 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com