Results 1 to 10 of 10

Thread: fill range based on Month

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Quote Originally Posted by Doug Robbins View Post
    Try

    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim numdays As Long
    Dim numyear As Long
    Dim i As Long
    If Target.Address = "$N$3" Then
        For i = 0 To 31
            Sheet1.Range("C8").Offset(i, 0) = ""
        Next i
        Select Case Format(Sheets(1).Range("N3"), "mmm")
            Case "Jan", "Mar", "May", "Jul", "Aug", "Oct", "Dec"
                numdays = 31
            Case "Apr", "Jun", "Sep", "Nov"
                numdays = 30
            Case "Feb"
                numyear = Format(Sheets(1).Range("N3"), "yyyy")
                If ((numyear Mod 4 = 0 And numyear Mod 400 = 0) Or (numyear Mod 4 = 0 And numyear Mod 100 <> 0)) Then 'Leap year
                    numdays = 29
                Else
                    numdays = 28
                End If
        End Select
        For i = 0 To numdays - 1
            Sheet1.Range("C8").Offset(i, 0) = "Data item"
        Next i
    End If
    End Sub
    Not sure what you wanted the range filled with, but replace "Data item" with your requirement.
    Kept debugging at : Select Case Format

  2. #2
    VBAX Contributor
    Joined
    Oct 2012
    Location
    Brisbane, Queensland, Australia
    Posts
    163
    Location
    If N3 is formatted as a date, it should work. If it is formatted as text then try replacing that line with

    SelectCase Left(Sheets(1).Range("N3"),3)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •