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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
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.

[TABLE="width: 370"]
<colgroup><col><col><col span="2"></colgroup><tbody>[TR]
[TD]Model[/TD]
[TD]Type[/TD]
[TD]Qty[/TD]
[TD]Month[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]9[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]11[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]12[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]9[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]11[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]12[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]9[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]11[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]12[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


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..

[TABLE="width: 370"]
<colgroup><col><col><col span="2"></colgroup><tbody>[TR]
[TD][TABLE="width: 372"]
<colgroup><col><col><col><col></colgroup><tbody>[TR]
[TD]Model[/TD]
[TD]Type[/TD]
[TD]Qty[/TD]
[TD]Month[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]Apr-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]May-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]Jun-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]Jul-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]Aug-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]Sep-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]Oct-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]Nov-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]Dec-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]Jan-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]Feb-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]A[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]Mar-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]Apr-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]May-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]Jun-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]Jul-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]Aug-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]Sep-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]Oct-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]Nov-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]Dec-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]Jan-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]Feb-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]B[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]Mar-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]1[/TD]
[TD="align: right"]Apr-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]2[/TD]
[TD="align: right"]May-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]3[/TD]
[TD="align: right"]Jun-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]4[/TD]
[TD="align: right"]Jul-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]Aug-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]6[/TD]
[TD="align: right"]Sep-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]7[/TD]
[TD="align: right"]Oct-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]8[/TD]
[TD="align: right"]Nov-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]Dec-2018[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]10[/TD]
[TD="align: right"]Jan-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]11[/TD]
[TD="align: right"]Feb-2019[/TD]
[/TR]
[TR]
[TD]W1[/TD]
[TD]C[/TD]
[TD="align: right"]12[/TD]
[TD="align: right"]Mar-2019[/TD]
[/TR]
</tbody>[/TABLE]
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
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,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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