HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Determining the number of days in a month

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
numberdeterminingthemonthdays

Problem

Overall logic: determine number of days in a month. Loop through table (wsUploadTable) and increment value if condition is met. Loop to next day in month and repeat.

For example: 10/1/2016, loops through table for date match and increment value. Next date, 10/2/2016 loop table for match... until last day of month 10/31/2016, loop table, find match and increment value.

'Determine DaysinMonth and assign DaysinMonth_Distro value
DaysInMonth = DateSerial(dtTrickle_Year, dtTrickle_Month + 1, 1) - _
              DateSerial(dtTrickle_Year, dtTrickle_Month, 1)
DoM_Distro = 1 / DaysInMonth

ReDim Days(1 To DaysInMonth)
For i = 1 To DaysInMonth
    Days(i) = DateSerial(dtTrickle_Year, dtTrickle_Month, i)

  'loop table and increment cell value if condition is met

    With wsUploadTable
    lngER_PrimaryID = .Cells(1048576, 2).End(xlUp).Row
        For intPrimaryID = 2 To lngER_PrimaryID
            'store current cell value
            dblLeadsValue = .Cells(intPrimaryID, col_Upload_Leads)
            'match UploadTable row on user input and increment new value 
              If.Cells(intPrimaryID, 3).Value = Days(i) Then
              .Cells(intPrimaryID, 11).Value = dblLeadsValue + DoM_Distro
            End If
        Next 'Next PrimaryID
    End With
Next i

Solution

Well havent tried either Excel or VBA , but the logic will go like this ,

  • Loop through the table



  • Say the date is day/month/year , where day,month and year are variables



  • lets have an array of months which is [31,28,31,30,31,30,31,31,30,31,30,31]



  • if year is divisible by 4 or 400 , then months[1]= months[1] + 1



  • now just display months[month]



Here months is the array and month is the number of month present in the date , for more info in arrays for VBA you can look here .

Context

StackExchange Code Review Q#143582, answer score: 2

Revisions (0)

No revisions yet.