On a worksheet I display the week number (periods are broken down into weeks - most have 4 but every once-in-a-while period 13 has 5 weeks). What I need to do before I fill that sheet with new data is look at that week number and decide what the next week number should be. That is easy for weeks 1, 2, and 3. When it comes to week 4 I have to look at another cell to see if it is period 13 or not. If not, then the week number is 1 (starting a new period). If the period is 13 it is possible there is a 5th week so I need to pop up some kind of box asking if there is, and based on a yes or no answer I'll decide what week it is (if yes then 5, if no then 1).
I started with a Select Case but when I tried to put an If Then statement into it there were problems - is it possible to do that? The If Then was in Case 4 and after there was Case 5 and when I tried to run it I got a message that said I had an End Select with no Select Case statement.
So that's why I thought I might need a function - I looked in Help and found one - but this is the first time I've tried a function and so I'm not sure if it is supposed to be separate (like another sub) and then I Call it or if I can put it inside.
Here's what I have:
Function NextWeek(WeekNumber, PeriodNumber)
Select Case WeekNumber
Case 1
WeekNumber = 2
MsgBox ("is this week right: " & WeekNumber)
Case 2
WeekNumber = 3
Case 3
WeekNumber = 4
Case 5
WeekNumber = 1
Case 4
If Worksheets("Forecast").Range("L3") <> 13 Then
WeekNumber = 1
Else
If Worksheets("Forecast").Range("L3") = 13 Then
Dim Msg, Style, Title, Response, MyString
Msg = "Will there be a 5th week in this period?" ' Define message.
Style = vbYesNo + vbDefaultButton2 ' Define buttons.
Title = "Decide if this is the 5th or 1st week" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
WeekNumber = 5
Else ' User chose No.
WeekNumber = 1
End If
End Select
End Function
Thank you.
I started with a Select Case but when I tried to put an If Then statement into it there were problems - is it possible to do that? The If Then was in Case 4 and after there was Case 5 and when I tried to run it I got a message that said I had an End Select with no Select Case statement.
So that's why I thought I might need a function - I looked in Help and found one - but this is the first time I've tried a function and so I'm not sure if it is supposed to be separate (like another sub) and then I Call it or if I can put it inside.
Here's what I have:
Function NextWeek(WeekNumber, PeriodNumber)
Select Case WeekNumber
Case 1
WeekNumber = 2
MsgBox ("is this week right: " & WeekNumber)
Case 2
WeekNumber = 3
Case 3
WeekNumber = 4
Case 5
WeekNumber = 1
Case 4
If Worksheets("Forecast").Range("L3") <> 13 Then
WeekNumber = 1
Else
If Worksheets("Forecast").Range("L3") = 13 Then
Dim Msg, Style, Title, Response, MyString
Msg = "Will there be a 5th week in this period?" ' Define message.
Style = vbYesNo + vbDefaultButton2 ' Define buttons.
Title = "Decide if this is the 5th or 1st week" ' Define title.
Response = MsgBox(Msg, Style, Title)
If Response = vbYes Then ' User chose Yes.
WeekNumber = 5
Else ' User chose No.
WeekNumber = 1
End If
End Select
End Function
Thank you.