PC Review


Reply
Thread Tools Rate Thread

Checking worksheet names

 
 
C Brehm
Guest
Posts: n/a
 
      7th Jan 2008
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


 
Reply With Quote
 
 
 
 
Rick Rothstein \(MVP - VB\)
Guest
Posts: n/a
 
      7th Jan 2008
Does this subroutine help you?

Sub GetYears(StartYear As Long, EndYear As Long)
Dim X As Long
StartYear = Right(Worksheets(1).Name, 4)
EndYear = Right(Worksheets(1).Name, 4)
For X = 2 To Worksheets.Count
If Worksheets(X).Name Like "Sales ####" Then
If Right(Worksheets(X).Name, 4) < StartYear Then
StartYear = Right(Worksheets(X).Name, 4)
End If
If Right(Worksheets(X).Name, 4) > EndYear Then
EndYear = Right(Worksheets(X).Name, 4)
End If
End If
Next
End Sub

You can use it like this from within your own code...

Sub Test()
Dim FirstYear As Long
Dim LastYear As Long
GetYears FirstYear, LastYear
MsgBox "Year range: " & FirstYear & " to " & LastYear
End Sub

Rick


"C Brehm" <quaker_molly-nospam-@netzero.net> wrote in message
news:(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      7th Jan 2008
Here is some code to extract the sheet number years

Dim FirstYear As Long
Dim LastYear As Long
Dim sh As Worksheet

For Each sh In ActiveWorkbook.Worksheets

If Left(sh.Name, 6) = "Sales " Then

If FirstYear = 0 Or Val(Right$(sh.Name, 4)) < FirstYear Then

FirstYear = Val(Right$(sh.Name, 4))
End If
If LastYear = 0 Or Val(Right$(sh.Name, 4)) > LastYear Then

LastYear = Val(Right$(sh.Name, 4))
End If
End If
Next sh


--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"C Brehm" <quaker_molly-nospam-@netzero.net> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Nigel
Guest
Posts: n/a
 
      7th Jan 2008
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
(E-Mail Removed)



"C Brehm" <quaker_molly-nospam-@netzero.net> wrote in message
news:(E-Mail Removed)...
> 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
>


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
using the Excel generic worksheet names instead of user-given names in code Paul Microsoft Excel Misc 5 26th Jun 2009 08:44 PM
how to insert workbook names and worksheet names to access columns =?Utf-8?B?Z29rb3A=?= Microsoft Access Queries 12 31st Aug 2007 03:52 PM
how to copy workbook names and worksheet names to columns in acces =?Utf-8?B?Z29rb3A=?= Microsoft Excel Programming 4 27th Aug 2007 11:26 AM
return all worksheet tab names and chart sheet tab names in report - an example DataFreakFromUtah Microsoft Excel Programming 2 6th Oct 2004 08:09 PM
checking names not checking michael montagne Microsoft Outlook 7 13th Aug 2004 01:22 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:57 AM.