Home » excel » excel – Add Days to Dates Unless Friday vba

excel – Add Days to Dates Unless Friday vba

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am using an Excel spreadsheet to populate a 6-day rotation calendar(Days 1-6). In the event of a snow day, we resume the next school day on the snow day’s schedule. I am trying to write vba to add 1 day to all highlighted (I manually highlight) dates unless it’s a Friday, then add 3.

Here is the code I have so far (I have also tried many other variations). It seems to skip over my If statement to the Else statement. (meaning it will add 1 day as intended, but not add 3 days to Friday). Your help would be appreciated. Thanks,Kim

Sub Add_Day_To_Range()

Dim c As Range
Dim Wkday As Integer
Wkday = Weekday(#1/6/2020#, vbMonday)

    For Each c In Selection.Cells
      If Wkday = 5 Then
      c.Value = c.Value + 3
      Else: c.Value = c.Value + 1
      End If
    Next c

End Sub
How to&Answers:

Adding the weekday into the loop should solve it. May not quite work depending on the date format in your cells but should be close

Sub Add_Day_To_Range()

Dim c As Range
Dim Wkday As Integer

For Each c In Selection.Cells
  Wkday = Weekday(c.Value, vbMonday)
  If Wkday = 5 Then
  c.Value = c.Value + 3
  Else: c.Value = c.Value + 1
  End If
Next c
End Sub