Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88835

While loop until a condition is met for every cell in a column

$
0
0

I am new to VBA and would like some help with a piece of code :)

Problem: Add 'Periodicity' values (with a nested if statement) to the 'Draft date' until the date is greater than or equal to the 'Cut off date'. Repeat for all cells in a column.

Referring to the example image: In cell S28 (5/09/2019), it has applied the formula of 'P28+14' but the adjusted date (19/09/2019) is still less than the cut off date (of 25/11/2019). The macro should repeat this periodicity formula until the adjusted date reaches 28/11/2019 (5 additional iterations).

Example Periodicity formula in cell S4:

=IF($G4="Weekly",P4+7,
   IF($G4="Fortnightly",P4+14,
     IF($G4="Monthly",EDATE(P4,1),
       IF($G4="2-Monthly",EDATE(P4,2),
        IF($G4="Quarterly",EDATE(P4,3),
          IF($G4="6-Monthly",EDATE(P4,6),"No"))))))

Corrected VBA code:

Function getModifiedDate(newdate, cutoff, periodicity)
  While newdate < cutoff
    If periodicity = "Monthly" Then
        newdate = DateAdd("m", 1, newdate)
    ElseIf periodicity = "2-Monthly" Then
        newdate = DateAdd("m", 2, newdate)
    ElseIf periodicity = "Quarterly" Then
        newdate = DateAdd("m", 3, newdate)
    ElseIf periodicity = "6-Monthly" Then
        newdate = DateAdd("m", 6, newdate)
    ElseIf periodicity = "Weekly" Then
        newdate = newdate + 7
    ElseIf periodicity = "Fortnightly" Then
        newdate = newdate + 14

    End If
  Wend
  getModifiedDate = newdate
End Function


Thank you in advance for any help, please ask for any further clarification if needed!

Example[Excel screenshot]

Solution[Solution screenshot]


Viewing all articles
Browse latest Browse all 88835

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>