Excel VBA-根据条件将表行复制到另一个表中,从而获得错误

时间:2016-05-02 18:13:59

标签: excel vba excel-vba

我尝试根据输入的数据类型创建具有不同颜色的甘特图,因此首先我必须将特定类型的数据复制到单独的图表中以协助格式化。但是当我这样做时,我会收到错误。我得到的当前错误是"溢出"。我的目标是复制其中" PROD"出现在另一张名为" Gantt Calc"动态的(因为先前的数据可能已存在)。这是代码:

 Sub test()
'
' test Macro
'


Dim i As Integer
Dim lastRow As Long

' Dim lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

'sorting by start date oldest-newest code block
Range("A4").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveWorkbook.Worksheets("Data Entry Page").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Data Entry Page").AutoFilter.Sort.SortFields.Add _
        Key:=Range("D4:D8"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("Data Entry Page").AutoFilter.Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

For i = 4 To Rows.Count 'May need to calibrate lastRow to make sense
    If ActiveSheet.Cells(i, "B").Value = "PROD" Then Range(i, "A:F").Copy (Sheets(2).Range(i))

Next i

End Sub

0 个答案:

没有答案