VBA Loop to clear contents of fiscal year rows in specific month columns

agonyrose

New Member
Joined
Oct 11, 2022
Messages
2
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I've been working on a VBA macro to clear the contents of certain month columns if they are in the current fiscal year or past fiscal year. I've gotten the variables and fiscal years to calculate correctly based on the July to June fiscal year, I just can't seem to figure out the correct structure of For Each or For If statement to loop through the Accounting Year (Column A) and clear the specific months rows in columns Q through AB.

Example scenario: Current Date is October 15 2022 (FY2023(Because after July)) I need to clear the current month of October (in Fiscal year 2023) and all months prior to October in Fiscal year 2022 to have 12 months of complete data.

Let me know if this is dumb and I should just continue doing this manually by sorting rows and clearing out the month columns. I've been staring at the code and googling VBA for loops(If and ForEach, and .clearcontents) for a week now and finally my stubbornness has resorted to asking for help.

Here is my VBA code so far. All of the code works but I have no idea how to structure the bottom two ForEach statements.
VBA Code:
Sub ExtractedCleanDatatest()
   
    'steps insert the formulas needed, rename the month usage to monthname'
    'run a loop to delete current month of currentFT and all months prior to current month past FY'
    'variables needed range of months, currentfy, past fy, currentmo, loop range,
   
    'dimensions (variables)'
    Dim Mo As Range
    Dim FY As Range
    Dim Pyx As Range
    Dim CurrentFY As Integer
    Dim PastFY As Integer
    Dim CurrentMonth As String
    Dim CurMo As Integer
    Dim ClearMo As Integer
   
    'constants'
    CurrentFY = IIf(Month(Date) <= 6, Year(Date), Year(Date) + 1)
    PastFY = IIf(Month(Date) <= 6, Year(Date), Year(Date))
    CurrentMonth = MonthName(Month(Now))
    CurMo = Month(Now)
    ClearMo = CurMo - 1 - 6
   
    'Calculation functions inserts columns from N-P labels columns inserts functions into N2--P2 fills down the functions'
   
    Range("N:P").EntireColumn.Insert
    Range("N1").Formula = "Lowest UOM Price"
    Range("O1").Formula = "12 Mo Use"
    Range("P1").Formula = "12 Mo Spend"
    Range("N2").Formula = "=L2/J2"
    Range("O2").Formula = "=SUM(Q2:AB2)"
    Range("P2").Formula = "=O2*N2"
    Range("N2:N" & Cells(Rows.Count, 12).End(xlUp).Row).FillDown
    Range("O2:O" & Cells(Rows.Count, 12).End(xlUp).Row).FillDown
    Range("P2:P" & Cells(Rows.Count, 12).End(xlUp).Row).FillDown
   
    'rename months Q-AB from July to June (fy order)
   
    Range("Q1").Formula = "July"
    Range("R1").Formula = "August"
    Range("S1").Formula = "September"
    Range("T1").Formula = "October"
    Range("U1").Formula = "November"
    Range("V1").Formula = "December"
    Range("W1").Formula = "January"
    Range("X1").Formula = "February"
    Range("Y1").Formula = "March"
    Range("Z1").Formula = "April"
    Range("AA1").Formula = "May"
    Range("AB1").Formula = "June"
   
    'loop to delete pyx rows in column c'
    Dim i As Integer
    For i = 5000 To 2 Step -1
        If Cells(i, 3).Value Like "*PYX*" Then
            Cells(i, 3).EntireRow.Delete
        End If
    Next i
   
    'clear contents of month columns based on FY row and current month'
    'For Each Cell In "A:A"
        'If Cell.Value = CurrentFY Then
            'clear row of current mo
    'Next
    'For Each Cell in "A:A"
        'If Cell.value = PastFY Then
            'Clear rows of Current month minus 1 to July
   
End Sub

Below is an example of my spreadsheet (headers are actual, but the othe data is sanitized)

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1Accounting Year Issue ToUsage Charge DeptUsage Charge Dept DescExpense CodeItem NoPrimary Vendor MfrItem DescMFR Item NoUOM Dft Purchase Conv FactorUOM Dft PurchaseUOM Dft Purchase PriceUOM LowestLowest UOM Price12 Mo Use12 Mo SpendJUL Usg QtyAUG Usg QtySEP Usg QtyOCT Usg QtyNOV Usg QtyDEC Usg QtyJAN Usg QtyFEB Usg QtyMAR Usg QtyAPR Usg QtyMAY Usg QtyJUN Usg Qty
22021206531deptabcSCxxxx123456vendorabcitemabc123410BX12EA1.240480000000000400
32022206532deptabcSCxxxx123457vendorabcitemabc123511BX13EA1.181922.50190000000000
42021206533deptabcSCxxxx123458vendorabcitemabc123612EA14EA1.1724280010300001901
52022206534deptabcSCxxxx123459vendorabcitemabc123713EA15EA1.151517.30015000000000
62021206535deptabcSCxxxx123460vendorabcitemabc123814EA16EA1.141921.70000000001090
72021206536deptabcSCxxxx123461vendorabcitemabc123915EA17EA1.132629.500031300001000
82021206537deptabcSCxxxx123462vendorabcitemabc124016EA18EA1.1311.13001000000000
92021206538deptabcSCxxxx123463vendorabcitemabc124117EA19EA1.1222.24100000000001
102021206539deptabcSCxxxx123464vendorabcitemabc124218BX20EA1.11-50-560-500000000000
112022206540deptabcSCxxxx123465vendorabcitemabc124319EA21EA1.1111.11100000000000
122021206541deptabcSCxxxx123466vendorabcitemabc124420EA22EA1.100000000000000
132021206542deptabcSCxxxx123467vendorabcitemabc124521EA23EA1.111.1000000001000
142021206543deptabcSCxxxx123468vendorabcitemabc124622EA24EA1.0911.09100000000000
152021206544deptabcSCxxxx123469vendorabcitemabc124723EA25EA1.0922.17000000002000
162021206545deptabcSCxxxx123470vendorabcitemabc124824EA26EA1.0811.08100000000000
172021206546deptabcSCxxxx123471vendorabcitemabc124925BX27EA1.082021.60000200000000
182021206547deptabcSCxxxx123472vendorabcitemabc125026BX28EA1.082021.50002000000000
192021206548deptabcSCxxxx123473vendorabcitemabc125127EA29EA1.071010.70100000000000
202021206549deptabcSCxxxx123474vendorabcitemabc125228EA30EA1.0722.14020000000000
212021206550deptabcSCxxxx123475vendorabcitemabc125329EA31EA1.071617.1050030040400
222022206551deptabcSCxxxx123476vendorabcitemabc125430EA32EA1.0744.27040000000000
232021206552deptabcSCxxxx123477vendorabcitemabc125531EA33EA1.063436.23400000000000
242022206553deptabcSCxxxx123478vendorabcitemabc125632CA34EA1.061010.61000000000000
252022206554deptabcSCxxxx123479vendorabcitemabc125733PK35EA1.061010.60100000000000
262021206555deptabcSCxxxx123480vendorabcitemabc125834CA36EA1.0610010600000505000000
272021206556deptabcSCxxxx123481vendorabcitemabc125935CA37EA1.0610010600000000010000
282021206557deptabcSCxxxx123482vendorabcitemabc126036EA38EA1.0658762000000337000200500
292021206558deptabcSCxxxx123483vendorabcitemabc126137EA39EA1.0530373201254240195344198242220282245241273303
302021206559deptabcSCxxxx123484vendorabcitemabc126238EA40EA1.052566270124425221932312210016428716735308345
312022206560deptabcSCxxxx123485vendorabcitemabc126339EA41EA1.05727764320296111000000000
322022206561deptabcSCxxxx123486vendorabcitemabc126440EA42EA1.0572075632829696000000000
332021206562deptabcSCxxxx123487vendorabcitemabc126541PK43EA1.05-10-10000000-1000000
342021206563deptabcSCxxxx123488vendorabcitemabc126642PK44EA1.05202100000100010000
352021206564deptabcSCxxxx123489vendorabcitemabc126743PK45EA1.0522.09000002000000
362021206565deptabcSCxxxx123490vendorabcitemabc126844CA46EA1.0510010500002010201004000
372021206566deptabcSCxxxx123491vendorabcitemabc126945CA47EA1.04-20-21000000-2000000
382021206567deptabcSCxxxx123492vendorabcitemabc127046EA48EA1.04229239000002117377658182
392021206568deptabcSCxxxx123493vendorabcitemabc127147EA49EA1.04229239000020048416832200
402021206569deptabcSCxxxx123494vendorabcitemabc127248EA50EA1.0411.04000000000001
412021206570deptabcSCxxxx123495vendorabcitemabc127349EA51EA1.040000-101000000000
422021206571deptabcSCxxxx123496vendorabcitemabc127450EA52EA1.042020.80200000000000
432021206572deptabcSCxxxx123497vendorabcitemabc127551EA53EA1.041717.7040010003900
442021206573deptabcSCxxxx123498vendorabcitemabc127652EA54EA1.043536.30600600108311
452022206574deptabcSCxxxx123499vendorabcitemabc127753EA55EA1.0444.15220000000000
462021206575deptabcSCxxxx123500vendorabcitemabc127854EA56EA1.0444.15030000000001
472022206576deptabcSCxxxx123501vendorabcitemabc127955EA57EA1.0411.04100000000000
482021206577deptabcSCxxxx123502vendorabcitemabc128056EA58EA1.0411.04001000000000
492022206578deptabcSCxxxx123503vendorabcitemabc128157EA59EA1.0411.04010000000000
502021206579deptabcSCxxxx123504vendorabcitemabc128258BX60EA1.032020.70000000000200
Extracted
Cell Formulas
RangeFormula
N2:N50N2=L2/J2
O2:O50O2=SUM(Q2:AB2)
P2:P50P2=O2*N2
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I need to clear the current month of October (in Fiscal year 2023) and all months prior to October in Fiscal year 2022 to have 12 months of complete data.
This is the bit I'm not following. If you cleared October (current FYTD) and all previous financial years' data, where does the 12 months of complete data come from? Could you define what those 12 months would be given your example scenario?
 
Upvote 0
This is the bit I'm not following. If you cleared October (current FYTD) and all previous financial years' data, where does the 12 months of complete data come from? Could you define what those 12 months would be given your example scenario?
It would be the prior 12 months to the current month of the current fiscal year. So if I pulled this report in october calendar year 2022, that would be FY2023, So I would clear the current month since it isn't a completed month of data and use the data from October FY22 to Sep FY23. I may have explained that strangely in the original post.

Try to add another sheet, sheet2, with your expected outcome from sheet1
Here is the expected outcome. Essentially what I do manually is sort column A to a single fiscal year, then clear the current month of the current fiscal year, then clear the months prior to the current month in the past fiscal year. That's kind of what I was using the "ClearMo" dimension for because it gives the amount of months back to clear for the past fiscal year.

vba code test.xlsx
ABCDEFGHIJKLMNOPQRSTUVWXYZAAAB
1Accounting Year Issue ToUsage Charge DeptUsage Charge Dept DescExpense CodeItem NoPrimary Vendor MfrItem DescMFR Item NoUOM Dft Purchase Conv FactorUOM Dft PurchaseUOM Dft Purchase PriceUOM LowestLowest UOM Price12 Mo Use12 Mo SpendJUL Usg QtyAUG Usg QtySEP Usg QtyOCT Usg QtyNOV Usg QtyDEC Usg QtyJAN Usg QtyFEB Usg QtyMAR Usg QtyAPR Usg QtyMAY Usg QtyJUN Usg Qty
22021206531deptabcSCxxxx123456vendorabcitemabc123410BX12EA1.240480000000000400
32022206532deptabcSCxxxx123457vendorabcitemabc123511BX13EA1.18181821922.4545450190000000000
42021206533deptabcSCxxxx123458vendorabcitemabc123612EA14EA1.16666672326.8333330000300001901
52022206534deptabcSCxxxx123459vendorabcitemabc123713EA15EA1.15384621517.3076920015000000000
62021206535deptabcSCxxxx123460vendorabcitemabc123814EA16EA1.14285711921.7142860000000001090
72021206536deptabcSCxxxx123461vendorabcitemabc123915EA17EA1.13333332629.46666700031300001000
82021206537deptabcSCxxxx123462vendorabcitemabc124016EA18EA1.12500000000000000
92021206538deptabcSCxxxx123463vendorabcitemabc124117EA19EA1.117647111.1176471000000000001
102021206539deptabcSCxxxx123464vendorabcitemabc124218BX20EA1.111111100000000000000
112022206540deptabcSCxxxx123465vendorabcitemabc124319EA21EA1.105263211.1052632100000000000
122021206541deptabcSCxxxx123466vendorabcitemabc124420EA22EA1.100000000000000
132021206542deptabcSCxxxx123467vendorabcitemabc124521EA23EA1.095238111.0952381000000001000
142021206543deptabcSCxxxx123468vendorabcitemabc124622EA24EA1.090909100000000000000
152021206544deptabcSCxxxx123469vendorabcitemabc124723EA25EA1.086956522.173913000000002000
162021206545deptabcSCxxxx123470vendorabcitemabc124824EA26EA1.083333300000000000000
172021206546deptabcSCxxxx123471vendorabcitemabc124925BX27EA1.082021.60000200000000
182021206547deptabcSCxxxx123472vendorabcitemabc125026BX28EA1.07692312021.5384620002000000000
192021206548deptabcSCxxxx123473vendorabcitemabc125127EA29EA1.074074100000000000000
202021206549deptabcSCxxxx123474vendorabcitemabc125228EA30EA1.071428600000000000000
212021206550deptabcSCxxxx123475vendorabcitemabc125329EA31EA1.06896551111.758621000030040400
222022206551deptabcSCxxxx123476vendorabcitemabc125430EA32EA1.066666744.2666667040000000000
232021206552deptabcSCxxxx123477vendorabcitemabc125531EA33EA1.064516100000000000000
242022206553deptabcSCxxxx123478vendorabcitemabc125632CA34EA1.06251010.6251000000000000
252022206554deptabcSCxxxx123479vendorabcitemabc125733PK35EA1.06060611010.6060610100000000000
262021206555deptabcSCxxxx123480vendorabcitemabc125834CA36EA1.0588235100105.8823500000505000000
272021206556deptabcSCxxxx123481vendorabcitemabc125935CA37EA1.0571429100105.7142900000000010000
282021206557deptabcSCxxxx123482vendorabcitemabc126036EA38EA1.0555556587619.6111100000337000200500
292021206558deptabcSCxxxx123483vendorabcitemabc126137EA39EA1.054054123482474.9189000344198242220282245241273303
302021206559deptabcSCxxxx123484vendorabcitemabc126238EA40EA1.052631618511948.421100032312210016428716735308345
312022206560deptabcSCxxxx123485vendorabcitemabc126339EA41EA1.0512821727764.28205320296111000000000
322022206561deptabcSCxxxx123486vendorabcitemabc126440EA42EA1.0572075632829696000000000
332021206562deptabcSCxxxx123487vendorabcitemabc126541PK43EA1.0487805-10-10.4878000000-1000000
342021206563deptabcSCxxxx123488vendorabcitemabc126642PK44EA1.0476192020.95238100000100010000
352021206564deptabcSCxxxx123489vendorabcitemabc126743PK45EA1.046511622.0930233000002000000
362021206565deptabcSCxxxx123490vendorabcitemabc126844CA46EA1.0454545100104.5454500002010201004000
372021206566deptabcSCxxxx123491vendorabcitemabc126945CA47EA1.0444444-20-20.88889000000-2000000
382021206567deptabcSCxxxx123492vendorabcitemabc127046EA48EA1.0434783229238.95652000002117377658182
392021206568deptabcSCxxxx123493vendorabcitemabc127147EA49EA1.0425532229238.74468000020048416832200
402021206569deptabcSCxxxx123494vendorabcitemabc127248EA50EA1.041666711.0416667000000000001
412021206570deptabcSCxxxx123495vendorabcitemabc127349EA51EA1.04081631010.4081630001000000000
422021206571deptabcSCxxxx123496vendorabcitemabc127450EA52EA1.0400000000000000
432021206572deptabcSCxxxx123497vendorabcitemabc127551EA53EA1.03921571313.509804000010003900
442021206573deptabcSCxxxx123498vendorabcitemabc127652EA54EA1.03846152930.1153850000600108311
452022206574deptabcSCxxxx123499vendorabcitemabc127753EA55EA1.037735844.1509434220000000000
462021206575deptabcSCxxxx123500vendorabcitemabc127854EA56EA1.03703711.037037000000000001
472022206576deptabcSCxxxx123501vendorabcitemabc127955EA57EA1.036363611.0363636100000000000
482021206577deptabcSCxxxx123502vendorabcitemabc128056EA58EA1.035714300000000000000
492022206578deptabcSCxxxx123503vendorabcitemabc128157EA59EA1.035087711.0350877010000000000
502021206579deptabcSCxxxx123504vendorabcitemabc128258BX60EA1.03448282020.6896550000000000200
Sheet1
Cell Formulas
RangeFormula
N2:N50N2=L2/J2
O2:O50O2=SUM(Q2:AB2)
P2:P50P2=O2*N2
 
Upvote 0
Here is the expected outcome.
I'm still not quite following it. For example, in your expected outcome, I see figures for April-2021. How is that part of the 12 months from September 2022 (last month of the current financial year to be included as per your clarification) going back 12 months?
 
Upvote 0
OK, give this a try on a copy of your data. I think it's what you're looking for.

VBA Code:
Option Explicit
Sub agonyrose()

    Dim EndDate As Date, StartDate As Date
    Dim CellDate As Long, FirstDate As Long, LastDate As Long, LRow As Long
    Dim DateString As String
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<<< change to actual sheet name
    
    'Do the formula stuff
    LRow = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    ws.Range("N:P").EntireColumn.Insert
    ws.Range("N1").Resize(, 3).Value = Array("Lowest UOM Price", "12 Mo Use", "12 Mo Spend")
    With ws.Range("N2:N" & LRow)
        .Formula = "=L2/J2"
        .Offset(, 1).Formula = "=SUM(Q2:AB2)"
        .Offset(, 2).Formula = "=O2*N2"
    End With
    
    'Change the month headers
    ws.Range("Q1").Resize(, 12).Value = Array("July", "August", "September", "October", _
    "November", "December", "January", "February", "March", "April", "May", "June")
    
    'Clear data not fitting into the required date range
    EndDate = WorksheetFunction.EoMonth(Now(), -1)
    LastDate = DateValue(EndDate)
    StartDate = WorksheetFunction.EoMonth(Now(), -13) + 1
    FirstDate = DateValue(StartDate)
    
    Dim ArrIn, ArrOut, i As Long, j As Long
    ArrIn = ws.Range("Q2:AB" & LRow)
    ArrOut = ws.Range("Q2:AB" & LRow)
    
    For i = 1 To UBound(ArrIn, 1)
        For j = 1 To UBound(ArrIn, 2)
            DateString = "1/" & Month(DateValue("1 " & ws.Cells(1, j + 16) & " 2000")) & "/" & ws.Cells(i + 1, 1).Value
            CellDate = DateValue(DateString)
                If CellDate >= FirstDate And CellDate <= LastDate Then
                    ArrOut(i, j) = ArrIn(i, j)
                Else
                    ArrOut(i, j) = vbNullString
                End If
        Next j
    Next i
    
    ws.Range("Q2").Resize(UBound(ArrOut, 1), UBound(ArrOut, 2)).Value = ArrOut

End Sub
 
Upvote 0
EDITED (added the delete "PYX" values from column C)

VBA Code:
Option Explicit
Sub agonyrose_2()

    Dim EndDate As Date, StartDate As Date
    Dim CellDate As Long, FirstDate As Long, LastDate As Long, LRow As Long
    Dim DateString As String
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")   '<<< change to actual sheet name
    
    'Do the formula stuff
    LRow = ws.Cells.Find("*", , xlFormulas, , 1, 2).Row
    ws.Range("N:P").EntireColumn.Insert
    ws.Range("N1").Resize(, 3).Value = Array("Lowest UOM Price", "12 Mo Use", "12 Mo Spend")
    With ws.Range("N2:N" & LRow)
        .Formula = "=L2/J2"
        .Offset(, 1).Formula = "=SUM(Q2:AB2)"
        .Offset(, 2).Formula = "=O2*N2"
    End With
    
    'Change the month headers
    ws.Range("Q1").Resize(, 12).Value = Array("July", "August", "September", "October", _
    "November", "December", "January", "February", "March", "April", "May", "June")
    
    'Get rid of the pyx rows in column C
    With ws.Range("A1").CurrentRegion
        .AutoFilter 3, "*PYX*"
        If Range("A:A").SpecialCells(xlCellTypeVisible).Rows.Count > 1 Then
            .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        End If
        .AutoFilter
    End With
    
    'Clear data not fitting into the required date range
    EndDate = WorksheetFunction.EoMonth(Now(), -1)
    LastDate = DateValue(EndDate)
    StartDate = WorksheetFunction.EoMonth(Now(), -13) + 1
    FirstDate = DateValue(StartDate)
    
    Dim ArrIn, ArrOut, i As Long, j As Long
    ArrIn = ws.Range("Q2:AB" & LRow)
    ArrOut = ws.Range("Q2:AB" & LRow)
    
    For i = 1 To UBound(ArrIn, 1)
        For j = 1 To UBound(ArrIn, 2)
            DateString = "1/" & Month(DateValue("1 " & ws.Cells(1, j + 16) & " 2000")) & "/" & ws.Cells(i + 1, 1).Value
            CellDate = DateValue(DateString)
                If CellDate >= FirstDate And CellDate <= LastDate Then
                    ArrOut(i, j) = ArrIn(i, j)
                Else
                    ArrOut(i, j) = vbNullString
                End If
        Next j
    Next i
    
    ws.Range("Q2").Resize(UBound(ArrOut, 1), UBound(ArrOut, 2)).Value = ArrOut

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,160
Members
453,021
Latest member
Justyna P

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