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.
 

Some videos you may like

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

MARK858

MrExcel MVP
Joined
Nov 12, 2010
Messages
13,323
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
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.
 

winde

New Member
Joined
Nov 27, 2018
Messages
32
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,800
Office Version
  1. 2013
Platform
  1. Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
16,800
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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?
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,049
Office Version
  1. 2016
Platform
  1. Windows
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
 

winde

New Member
Joined
Nov 27, 2018
Messages
32

ADVERTISEMENT

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>
 

KennyGreens

Board Regular
Joined
Aug 8, 2018
Messages
142
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:

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,049
Office Version
  1. 2016
Platform
  1. Windows
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
 

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,049
Office Version
  1. 2016
Platform
  1. Windows
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
 

Watch MrExcel Video

Forum statistics

Threads
1,108,652
Messages
5,524,098
Members
409,559
Latest member
rjoslin414

This Week's Hot Topics

Top