Excel 2007 VBA copy rows x times based on text filter

前端 未结 2 772
孤独总比滥情好
孤独总比滥情好 2021-01-24 22:30

I\'m new to VBA and can;t wrap my head around the most efficient way to do this - what I\'m looking for is a way to copy my data into rows below the active cell based upon a fre

2条回答
  •  情话喂你
    2021-01-24 23:31

    You need a function that translate the frequency text to a number of months (let´s call it MonthFreq returning an integer).

    This will do what you want:

    MaxDate = DateSerial(2013, 4, 1)
    Do Until Origin.Cells(OriginRow, NameColumn).Value = ""
        SourceDate = Origin.Cells(OriginRow, DateColumn).Value
        Do Until SourceDate >= MaxDate
            ' Copy origin row to destiny.
            Destiny.Cells(DestinyRow, DateColumn).Value = SourceDate
    
            SourceDate = DateAdd("m", MonthFreq(Origin.Cells(OriginRow, FreqColumn).Value), SourceDate)
            DestinyRow = DestinyRow + 1
        Loop
        OriginRow = OriginRow + 1
    Loop
    

    Origin is the worksheet with the original data, Destiny is the worksheet where the expanded data will be saved. OriginRow is the current row being analyzed in the Origin worksheet (starts at the first row). OriginColumn is the current row being written in the Destiny worksheet (starts at the first row). SourceDate will be added some number of months until it reaches the MaxDate.

提交回复
热议问题