基于两个日期复制记录

时间:2017-12-24 18:33:57

标签: excel-vba vba excel

我想更改下表

输入

Facility       From Date      To Date    DAYS
D             11-Sep-2017     13-Sep-17    3
C             26-Nov-2017     29-Nov-17    4

结果

Facility       From Date    DAYS
 D          11-Sep-2017     1
 D          12-Sep-2017     1
 D          13-Sep-2017     1
 C          26-Nov-2017     1
 C          27-Nov-2017     1
 C          28-Nov-2017     1
 C          29-Nov-2017     1

1 个答案:

答案 0 :(得分:0)

我设法让它运作

Dim Marker As Integer
Marker = 2
While Marker > 1
    Marker = Range("H2").Value

For i = 1 To Marker - 1
    Rows("2:2").Select
    Selection.Copy
    Rows("3:3").Select
    Selection.Insert Shift:=xlDown
Next i

Range("H2:H" & Marker + 1).Value = 1
Range("F3:F" & Marker + 1).FormulaR1C1 = "=R[-1]C+1"
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
    ("H1:H2061"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
    xlSortTextAsNumbers
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Wend