没有错误,但是宏可以逐行使用F8,而不是在执行完整宏时使用-Excel,VBA

时间:2018-08-30 19:23:37

标签: excel vba excel-vba

我在一个工作簿中有6个相同的宏。 6人中有4人表现良好,但其余的我也有同样的问题。

如果使用F8在调试窗口中运行宏,则可得到理想的结果。如果我正常运行宏,则不会有任何错误,但是结果显然是错误的。 我可以猜测在那种情况下,macto会忽略这部分(所有错误都从这里开始),但不确定

ActiveSheet.Range("H2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])"
ActiveSheet.Range("H2").Select
Selection.AutoFill Destination:=Range("H2:H" & lastrow)
ActiveSheet.Range("H2:H" & lastrow).Select

宏的目标是过滤一个选项卡,在另一选项卡中放入几列;将其中一列中的值与另一个标签进行比较,删除重复项,过滤结果并将其粘贴到“结果”标签中。 当我手动执行此操作时,“结果”选项卡中有6行。正常运行时,我只有一行,或者什么也没有。.

请您指教-此宏有什么问题?

我试图将这一行添加到我的代码中(不走运): Application.PrintCommunication = True

我尝试放置DoEvents                     每行,每列和每个范围之前都有ThisWorkbook-没有运气

非常感谢!

这是我的完整代码:

Public lastrow As Long
Public FileName As String
Public TabName As String

Sub APP_filtering_new()
'
' APP_filtering Macro


lastrow = ActiveSheet.Range("A1048576").End(xlUp).Row

Sheets("APP-input").Select

    ActiveSheet.Rows("1:1").Select
    Selection.AutoFilter
    ActiveSheet.Range("$A$1:$AG$14878").AutoFilter Field:=2, Criteria1:=Array( _
        "BRAMPTON", "VANCOUVER, CD", "VANCOUVER", _
        "VANCOUVER TERMINAL"), Operator:=xlFilterValues
    ActiveSheet.Columns("E:E").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Sheets("APP_output").Select
    ActiveSheet.Columns("A:A").Select
    ActiveSheet.Paste
    Sheets("APP-input").Select
    ActiveSheet.Columns("N:N").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("APP_output").Select
    ActiveSheet.Columns("D:D").Select
    ActiveSheet.Paste
    Sheets("APP-input").Select
    ActiveSheet.Columns("G:G").Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Application.CutCopyMode = False
    Selection.Copy
    Sheets("APP_output").Select
    ActiveSheet.Columns("E:E").Select
    ActiveSheet.Paste
    ActiveSheet.Range("F2").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = " "
    ActiveSheet.Range("F2").Select
    Selection.AutoFill Destination:=Range("F2:F" & lastrow)
    ActiveSheet.Range("F2:F" & lastrow).Select
    ActiveSheet.Range("G2").Select
    ActiveCell.FormulaR1C1 = " "
    ActiveSheet.Range("G2").Select
    Selection.AutoFill Destination:=Range("G2:G" & lastrow)
    ActiveSheet.Range("G2:G" & lastrow).Select
    ActiveSheet.Range("H2").Select
    ActiveCell.FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])"
    ActiveSheet.Range("H2").Select
    Selection.AutoFill Destination:=Range("H2:H" & lastrow)
    ActiveSheet.Range("H2:H" & lastrow).Select
    ActiveSheet.Columns("H:H").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

       'remove duplicates

    ActiveSheet.Columns("A:H").Select
    Application.CutCopyMode = False
    ActiveSheet.Range("A1:E" & lastrow).RemoveDuplicates Columns:=5, Header:= _
        xlNo

        'vlookup, IF condition

    ActiveSheet.Range("I2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-4],container,4,FALSE)"
    ActiveSheet.Range("I2").Select
    Selection.AutoFill Destination:=Range("I2:I" & lastrow)
    ActiveSheet.Range("I2:I" & lastrow).Select
    ActiveSheet.Range("J2").Select
    ActiveCell.FormulaR1C1 = _
        "=IF(RC[-1]<RC[-2],""C. has bigger number of Containers"",IF(RC[-1]=RC[-2],""The same amount of containers"",IF(RC[-2]<RC[-1],""The C. has less amount of Containers"")))"
    ActiveSheet.Range("J2").Select
    Selection.AutoFill Destination:=Range("J2:J" & lastrow)
    ActiveSheet.Range("J2:J" & lastrow).Select
    ActiveSheet.Range("H1").Select
    ActiveCell.FormulaR1C1 = "Amt of Containers - External report"
    ActiveSheet.Range("I1").Select
    ActiveCell.FormulaR1C1 = "Amt of Containers - Internal report"
    ActiveSheet.Range("J1").Select
    ActiveCell.FormulaR1C1 = "Result (N/A means New Shipment)"
    ActiveSheet.Range("H1:J1").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlTop
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    ActiveSheet.Range("H1:I1").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.Range("J1").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.Range("J1").Select
    Application.CutCopyMode = False
    Selection.AutoFilter
    ActiveSheet.Range("D1:J" & lastrow).AutoFilter Field:=7, Criteria1:=Array( _
        "#N/A", "C. has bigger number of Containers", _
        "The C. has less amount of Containers"), Operator:=xlFilterValues

     ' paste in next empty row

    ActiveSheet.Rows("2:2").Select
    ActiveSheet.Range(Selection, Selection.End(xlDown)).Select
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy

     Sheets("Results").Select
    lastrow = ActiveSheet.Range("A1048576").End(xlUp).Row

    ActiveSheet.Range("A" & lastrow + 1).Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

     End Sub

1 个答案:

答案 0 :(得分:0)

这不是一个完整的答案,但是例如,这段代码

ActiveSheet.Range("H2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])"
ActiveSheet.Range("H2").Select
Selection.AutoFill Destination:=Range("H2:H" & lastrow)
ActiveSheet.Range("H2:H" & lastrow).Select

可以用一行代替

ActiveSheet.Range("H2:H" & lastrow).FormulaR1C1 = "=COUNTIF(C[-3],RC[-3])"

摆脱ActiveSheet并替换为实际的工作表名称。