Help with Select Case

kakiebos

Board Regular
Joined
Jun 10, 2018
Messages
62
Hi All, I need help with a Select Case in my code. I'm new to VBA and have never used the Select Case function. With my online research, I found some examples, but none is really giving me the information that I need.

When the user call a UserForm (frmResource) from any one of the sheets Monday to Sunday a label (lDay) in the form indicate to the user which day they are in. The information that they enter in this UserForm is then sent to that day's sheet. Some of this information is then captured on a weekly summary sheet (Object name: shSummary). In the weekly summary I want to add the date to a certain position, depending on the day of the week.

I was thinking that the Select Case code would do the job the best, but something is not working for me.
Sometimes it will add the date for Saturday on the Monday position in the weekly summary

Can someone give me some insight as to why it is not working as expected.

For additional information, I prefer to use the sheets object name in my code and each of the daily sheets is called as shMon; shTue etc.
Code:
Dim shSum As Worksheet
Dim Day As Integer


'Add date to summary sheet
    Select Case Day
      Case Me.lDay = "Monday"
        shSum.Range("B12").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
      Case Me.lDay = "Tuesday"
        shSum.Range("B14").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
      Case Me.lDay = "Wednesday"
        shSum.Range("B16").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
      Case Me.lDay = "Thursday"
        shSum.Range("B18").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
      Case Me.lDay = "Friday"
        shSum.Range("B20").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
      Case Me.lDay = "Saturday"
        shSum.Range("B22").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
      Case Me.lDay = "Sunday"
        shSum.Range("B24").Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
    End Select
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Do you mean something like this instead? Notice the use of LCase so make all your days lower case.

Code:
Select Case LCase(Me.lDay)
    Case "monday"
        'Do this
    Case "tuesday"
        'Do that
End Select
 
Upvote 0
You could also see if this does the same thing:

Code:
dayInt = Application.Match(Me.lDay, Array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"), 0)
shSum.Range("B" & 2 * dayInt + 10).Value = CStr(Format(Me.lDate, "yyyy-mm-dd"))
 
Upvote 0
Hi Steve.

Thank you for your assistance. The first set of code was not working, I think it has something to do with my code to enter the date in the summary sheet. Perhaps I did not have my syntax correct. I will still look into that.

Your second code that you gave me is working perfectly and looks much neater than the code that I had in mind. Just to confirm dayInt, do I dim it as Array?
 
Upvote 0
No it's an integer. Match will produce the position in the array. 1 for monday 2 for tuesday etc.
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,871
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top