Search code examples
vbaskip

Skip cells while filling values


I am trying to fill cells from D3 to AH30 with "1".And I have dates in D1 to AH1 cells . But I do not need to fill the cells with "1" if corresponding cell in first row has a weekend date in it.

I tried below code. But its filling "1" in all cells including weekend days

    Function CheckWeekday_and_fill()
    Dim dDate As Date
     dDate = Range("D1").Value       
    For row = 3 To 30 ' rows for cells with date in it
    For col = 4 To 34 ' cols for cells with date in it

    Select Case Weekday(dDate)
    Case vbSaturday, vbSunday
    Cells(row, col).Value = " "
    Case Else
    Cells(row, col).Value = "1"
    End Select
    dDate = dDate + 1
    Next col
    Next row                    
    End Function

Solution

  • Try this:

    Function CheckWeekday_and_fill()
    
    Dim dDate As Date      
    For row = 3 To 30 ' rows for cells with date in it
    For col = 4 To 34 ' cols for cells with date in it
        dDate = Cells(1, col).Value
        Select Case Weekday(dDate)
        Case vbSaturday, vbSunday
        Cells(row, col).Value = " "
        Case Else
        Cells(row, col).Value = "1"
        End Select
    Next col
    Next row                    
    
    End Function