vba loop through month and year

winde

New Member
Joined
Nov 27, 2018
Messages
32
Hi

i need a code to loop my data in column H in terms of month and year till its last row.

so technically it should look like this

Column H
apr-18
may-18
jun-18
jul-18
aug-18
sep-18
oct-18
nov-18
dec-18
jan-19
feb-19
mar-19
apr-18
may-18
jun-18
jul-18
aug-18
sep-18
oct-18
nov-18
dec-18
jan-19
feb-19
mar-19

Thanks.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Loop your data and do what? What does your original data look like? The table you have posted (if they are real dates and not text) is just a normal sort oldest to newest.
 
Upvote 0
Loop your data and do what? What does your original data look like? The table you have posted (if they are real dates and not text) is just a normal sort oldest to newest.


basically my data does not have any month and year tag to any row..i need the month and year there so i can do a pivot to it.

so starting from row 2 to my last row i need to input the month and year in repetitive of Apr18 to Mar19.

of coz i can do a copy and paste to it..but i wan to incorporate it into my existing vba code
 
Upvote 0
Are you saying in column H you want to enter dates

Starting in Row(1) with April 2018 and in Range("H1") With "April 2018" and in Range("H2") May 2018

And how many more?

If so tell us now exactly how you want it to look.

Your visual does not look very nice:

Maybe it should Be

April 2018
 
Upvote 0
You said:
so starting from row 2 to my last row

Well the last row in column H will be 1.5 million

Do we really need 1.5 million cells in column H with dates in them?
 
Upvote 0
Maybe :
Code:
Sub FillDates()
Dim lr&
If Not IsDate([H2]) Then
    MsgBox "Enter a valid date in H2"
    Exit Sub
End If
lr = Cells.Find(What:="*", After:=Cells(1), _
    Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row
With [H2]
    .AutoFill .Resize(lr - 1), xlFillMonths
    .Resize(lr - 1).NumberFormat = "mmm yyyy"
End With
End Sub
 
Upvote 0
the last row i meant is my last data row which would be expanding.

what i have now looks sth like this.

ModelTypeQtyMonth
W1A1
W1A2
W1A3
W1A4
W1A5
W1A6
W1A7
W1A8
W1A9
W1A10
W1A11
W1A12
W1B1
W1B2
W1B3
W1B4
W1B5
W1B6
W1B7
W1B8
W1B9
W1B10
W1B11
W1B12
W1C1
W1C2
W1C3
W1C4
W1C5
W1C6
W1C7
W1C8
W1C9
W1C10
W1C11
W1C12

<colgroup><col><col><col span="2"></colgroup><tbody>
</tbody>


so what im trying to do is..i wan to know the number of quantity for a model by month based on their type.

the end result should be something like..

ModelTypeQtyMonth
W1A1Apr-2018
W1A2May-2018
W1A3Jun-2018
W1A4Jul-2018
W1A5Aug-2018
W1A6Sep-2018
W1A7Oct-2018
W1A8Nov-2018
W1A9Dec-2018
W1A10Jan-2019
W1A11Feb-2019
W1A12Mar-2019
W1B1Apr-2018
W1B2May-2018
W1B3Jun-2018
W1B4Jul-2018
W1B5Aug-2018
W1B6Sep-2018
W1B7Oct-2018
W1B8Nov-2018
W1B9Dec-2018
W1B10Jan-2019
W1B11Feb-2019
W1B12Mar-2019
W1C1Apr-2018
W1C2May-2018
W1C3Jun-2018
W1C4Jul-2018
W1C5Aug-2018
W1C6Sep-2018
W1C7Oct-2018
W1C8Nov-2018
W1C9Dec-2018
W1C10Jan-2019
W1C11Feb-2019
W1C12Mar-2019

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>

<colgroup><col><col><col span="2"></colgroup><tbody>
</tbody>
 
Upvote 0
Hi there, you can try something like this:

Code:
Sub test()

Dim rmonth As Variant
Dim i As Integer
Dim lrow As Long
Dim x As Integer


rmonth = Split("April 2018,May 2018,June 2018,July 2018,August 2018,September 2018,October 2018,November 2018,December 2018,January 2019,February 2019,March 2019", ",")

lrow = Sheets(1).Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row

x = 0


For i = 2 To lrow
    If x < 11 Then
        Range("D" & i).Value = rmonth(x)
        x = x + 1
    Else
        Range("D" & i).Value = rmonth(x)
        x = 0
    End If
Next i
End Sub
 
Last edited:
Upvote 0
If each type always consists of 12 rows (like your example) then :
Code:
Sub FillDates()
Dim lr&
If Not IsDate([H2]) Then
    MsgBox "Enter a valid date in H2"
    Exit Sub
End If
lr = Cells.Find(What:="*", After:=Cells(1), _
    Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
    SearchDirection:=xlPrevious, MatchCase:=False).Row
With [H2]
    .AutoFill .Resize(12), xlFillMonths
    .Resize(12).Copy [H2].Resize(lr - 1 + (lr - 1) Mod 12)
    .Resize(lr - 1).NumberFormat = "mmm yyyy"
End With
End Sub
 
Upvote 0
Revised :
Code:
Sub FillDates()
Dim lr&
If Not IsDate([H2]) Then
    MsgBox "Enter a valid date in H2"
    Exit Sub
End If
lr = Cells(Rows.Count, "D").End(xlUp).Row
With [H2]
    .AutoFill .Resize(12), xlFillMonths
    .Resize(12).Copy [H2].Resize(lr - 1)
    .Resize(lr - 1).NumberFormat = "mmm yyyy"
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,544
Messages
6,114,249
Members
448,556
Latest member
peterhess2002

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