Search code examples
vbaif-statementconditional-statementsweekday

VBA Conditional IF statement using WEEKDAY


Screenshot of Excel Columns

Screenshot of Excel Columns

I am trying to write a conditional IF statement based on the day of the week.

  • Column A has the date.

  • Column AK has a weekend value.

  • Column AL has a weekday value.

I need the correct value (depending on day of week) to flow into column AT. The code I came up with isn't working. All that's showing up in AT is the AK value. I'm pretty new to VBA, I'm definitely missing something. Thanks in advance for any help.

Sub Weekday()

For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row

If Range("A" & i) = "=Weekday" Then 'This line of code doesn't work
     
Range("AT" & i).Value = Range("AL" & i).Value 'This line of code works
                                    
Else
Range("AT" & i).Value = Range("AK" & i).Value 'This line of code works

End If

Next

End Sub

UPDATE: the AK and AL values are both decimals, like 506.468 or 348.286


Solution

  • The VBA formula for returning the day of the week is slightly different than the worksheet formula. Try

    If  Weekday(Range("A" & i).value, vbMonday) < 6 Then
    

    Further information on this function and it's arguments.