View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Nigel[_2_] Nigel[_2_] is offline
external usenet poster
 
Posts: 735
Default Checking worksheet names

One way.....


Sub MinMax()

Dim firstYear As Integer, lastYear As Integer
Dim wS As Worksheet, sheetYear As Integer

For Each wS In Worksheets
If Left(wS.Name, 5) = "Sales" Then
sheetYear = Val(Right(wS.Name, 4))
If firstYear = 0 Then firstYear = sheetYear
If sheetYear < firstYear Then firstYear = sheetYear
If sheetYear lastYear Then lastYear = sheetYear
End If
Next

' useful check
MsgBox "First Year: " & firstYear & vbCrLf & _
"Last Year: " & lastYear

End Sub


--

Regards,
Nigel




"C Brehm" wrote in message
...
Have the folowing code hat works, but sheet name may not exsit.
All worksheets with data start with "Sales " and end in a year
i.e. "Sales 2005"
Need to get first year of worksheets and last year and make the years
between first year and last year only valid for years that exsit in
workbook.
How would I check for worksheet names and get Firstyear and Lastyear?


Do
SYear = CLng(Application.InputBox(Prompt:="Enter a year between
2005 and 2050", Default:=Year(Date), Type:=1))
' get start year for report
If SYear = 0 Then
Exit Sub 'give the user a way out??
Else
If SYear = 2005 And SYear <= 2050 Then
Exit Do
End If
End If
Loop