[VBA] Roll back through months in a loop

RockandGrohl

Well-known Member
Joined
Aug 1, 2018
Messages
790
Office Version
  1. 365
Platform
  1. Windows
Hi, really not sure how to make the title sound like what I want to do.

I have a user-set date which is input as Mmm-YY (Oct-21 for instance) - when placed in a cell it's listed as 01/10/2021 which is absolutely fine, the format is set as "Oct-21" so it's perfect.

I'd like to put the previous 5 months in cells to the left. Say Oct-21 is in cell L4, I'd like a macro to just but Sep-21 in K4, Aug-21 in J4 etc until it gets to May-21 where it inputs that and stops.


This is the sort of thing I could do with my eyes closed but after an extended break from VBA, I simply can't remember :( Sad face.


EDIT: I'm a long way off testing, would this work bychance?

VBA Code:
RepDate = InputBox("Please enter date in MM-YY format", "Enter Date")
Range("L4").Value = Format(RepDate, "Mmm-YY")

Range("K4").Activate
x = -1
Do Until Cells(ActiveCell.Column) = 7
    ActiveCell.Value = Format(DateAdd("m", x, RepDate), "Mmm-YY")
    x = x - 1
ActiveCell.Offset(0, -1).Activate
Loop

I need RepDate to take 10-21 and convert it to 01/10/2021
 
Last edited:

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I'm a long way off testing, would this work bychance?
Very close.

My approach will be something like the following, still using the DateAdd function as you did. However, the Format function in VBA won't help you display the cell value as you need. We actually need to format the cells instead.

Note: I am not quite sure if you need to go back 6 months or always until May, but I assumed it is 6 months starting from the input in the code below. If you need otherwise, then please let us know.

VBA Code:
Sub doIt()
Dim i As Integer
Dim rng As Range
Dim userInput
Dim RepDate As Date

    ' Ask for the user input and split by using dash as parameter
    userInput = InputBox("Please enter date in MM-YY format", "Enter Date")
    userInput = Split(userInput, "-")
    
    ' Create the RepDate by using the input fragments
    RepDate = DateSerial(userInput(1), userInput(0), 1)
    
    ' Better to use a variable for the range
    ' instead of calling it with the Range function with address all the time
    Set rng = Range("L4")
    rng.Value = RepDate
    rng.NumberFormat = "mmm-yy"
    
    ' Oct to May, 6 months. Oct is the starting cell.
    ' So we need 5 cells  back to May
    For i = 1 To 5
        With rng.Offset(, -i)
            .Value = DateAdd("m", -i, RepDate)
            .NumberFormat = "mmm-yy"
        End With
    Next i
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,973
Messages
6,122,534
Members
449,088
Latest member
RandomExceller01

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top