if Then Day

billandrew

Well-known Member
Joined
Mar 9, 2014
Messages
743
I am wondering if there is an easier way to return the first letter of the weekday in VBA other than what what I have here.

For X = 1 to 1000

If cells(x,2) = "Monday" then
cells(x,3) = "M")
Elseif cells(x,2 = "Tuesday" then
cells(x,3) = "T" and on to Sunday...

Thanks
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Why can you not tell us exactly what you want?

If value= "Monday" then Value= "M"
If Value= "Tuesday then value= "Cake"


Telling us you want unique values just has us guessing what you want.
 
Upvote 0
My apologies....

if value = Thursday then value = "H"
if value = Sunday then value = "U"

TY

Code:
Sub f()
Dim r$: r = [B1:B1000].Address
[C1:C1000] = Evaluate(Replace("=If(#=""Thursday"",""H"",If(#=""Sunday"",""U"",Left(#,1)))", "#", r))
End Sub
 
Upvote 0
Try this:

Code:
Sub Check_Weekday()
'Modified 7-18-17 12:57 AM EDT
Application.ScreenUpdating = False
Dim i As Long
    For i = 1 To 1000
    With Cells(i, 3)
        MyVal = Cells(i, 2).Value
            Select Case MyVal
                Case "Sunday"
                    .Value = "U"
                Case "Monday"
                    .Value = Left(Cells(i, 2), 1)
                Case "Tuesday"
                    .Value = Left(Cells(i, 2), 1)
                Case "Wednesday"
                    .Value = Left(Cells(i, 2), 1)
                Case "Thursday"
                    .Value = "H"
                Case "Friday"
                    .Value = Left(Cells(i, 2), 1)
                Case "Saturday"
                    .Value = Left(Cells(i, 2), 1)
            End Select
        End With
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Works great!!!
I think this much shorter code should also work for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub FirstWeekdayLetter()
  [C1:C1000] = [INDEX(MID("UMTWHFS",(FIND(LEFT(B1:B1000&"  ",2),"SuMoTuWeThFrSa  ")+1)/2,1),)]
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
Mr. Rothstein

The code worked with the exception of the first row which contains a title "Day" returned #VALUE error
 
Upvote 0
Mr. Rothstein

The code worked with the exception of the first row which contains a title "Day" returned #VALUE error
Then change the C1 to C2 and the B1 to B2 in order to skip the first row which is apparently contains header cells.
Code:
Sub FirstWeekdayLetter()
  [C2:C1000] = [INDEX(MID("UMTWHFS",(FIND(LEFT(B2:B1000&"  ",2),"SuMoTuWeThFrSa  ")+1)/2,1),)]
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,632
Messages
6,120,652
Members
448,975
Latest member
sweeberry

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