编写宏来组织大量信息

时间:2018-06-05 10:34:59

标签: excel-vba vba excel

我目前正在尝试将一个漫长乏味的过程变成一个工作宏。下面的宏代码是通过记录上述过程生成的,但是,当我在原始信息上运行生成的宏时,我没有得到正确的结果。有人可以帮帮我吗?

Sub Macro1()
'
' Macro1 Macro

Columns("B:B").Select
Selection.Cut
Columns("A:A").Select
Selection.Insert Shift:=xlToRight
ActiveCell.FormulaR1C1 = "Order No."
Columns("C:C").Select
Selection.Cut
Columns("B:B").Select
Selection.Insert Shift:=xlToRight
Columns("C:C").Select
Sheets("data").Select
Columns("C:C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:E").Select
Selection.Cut
Columns("D:D").Select
Selection.Insert Shift:=xlToRight
Columns("E:E").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Sheets("data").Select
Selection.Delete Shift:=xlToLeft
Sheets("data").Select
Selection.Delete Shift:=xlToLeft
Sheets("data").Select
Selection.Delete Shift:=xlToLeft
Sheets("vybrané sloupce").Select
Range("L5499").Select
Range("L1").Select
Sheets("data").Select
Sheets("data").Select
Columns("F:F").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("G:G").Select
Selection.Delete Shift:=xlToLeft
Columns("H:H").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Selection.Delete Shift:=xlToLeft
Columns("K:K").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("L:L").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("M:M").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
Columns("M:AP").Select
Selection.Delete Shift:=xlToLeft
Range("N1").Select
Sheets("vybrané sloupce").Select
Range("M1").Select
Sheets("data").Select
Columns("N:N").Select
Selection.Delete Shift:=xlToLeft
Range("P6").Select
End Sub
Sub Macro2()
'
' Macro2 Macro
'
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$M$12397").AutoFilter Field:=2, Criteria1:=Array( _
    "CONF-@", "CONF-MC", "CONF-PAY"), Operator:=xlFilterValues
Range("A452").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete
ActiveWindow.SmallScroll Down:=-12
ActiveSheet.Range("$A$1:$M$12158").AutoFilter Field:=2
ActiveWindow.SmallScroll Down:=-12
ActiveSheet.Range("$A$1:$M$12158").AutoFilter Field:=8, Criteria1:=Array( _
    "icprdv01", "ICPRMJ04", "ICPRPG01", "kowalska", "MONITORI01", "="),         Operator:= _
    xlFilterValues
ActiveWindow.SmallScroll Down:=-9
Range("A2").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=8
Range("B4").Select
End Sub
Sub Macro3()
'
' Macro3 Macro
'

ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=10, Criteria1:= _
    "Cancelled"
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=6, Criteria1:= _
    "Atradius Quick Research"
Range("J2947").Select
ActiveCell.FormulaR1C1 = "OK"
Range("J2947").Select
Selection.FillDown
Range("J4740").Select

End Sub
Sub Macro4()
'
' Macro4 Macro
'
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=10
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=6
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=2, Criteria1:= _
    "Atradius Initial Research"
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=6, Criteria1:= _
    "CREDIT REPORT"
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=10, Criteria1:= _
    "Cancelled"
Range("J3645").Select
ActiveCell.FormulaR1C1 = "OK"
Range("J3645").Select
Selection.FillDown
Range("J4614").Select
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=10
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=6
ActiveSheet.Range("$A$1:$M$6445").AutoFilter Field:=2
End Sub
Sub Macro5()
'
' Macro5 Macro
'
Range("N1").Select
ActiveCell.FormulaR1C1 = "Compare"
Range("N2").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-3]<>RC[-1],""No Match"",""Match"")"
Selection.AutoFill Destination:=Range("N2:N6445"), Type:=xlFillDefault
Range("N2:N6445").Select
Range("N1").Select
Selection.AutoFilter
Selection.AutoFilter
ActiveSheet.Range("$A$1:$N$6445").AutoFilter Field:=14, Criteria1:="Match"
Range("J7").Select
ActiveSheet.Range("$A$1:$N$6445").AutoFilter Field:=10, Criteria1:= _
    "Cancelled"
Range("J432").Select
ActiveCell.FormulaR1C1 = "OK"
Range("J432").Select
Selection.FillDown
Range("J3790").Select
ActiveSheet.Range("$A$1:$N$6445").AutoFilter Field:=10
Range("N1").Select
ActiveSheet.Range("$A$1:$N$6445").AutoFilter Field:=14
ActiveSheet.Range("$A$1:$N$6445").AutoFilter Field:=10, Criteria1:= _
    "Cancelled"
Range("A2718").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.EntireRow.Delete
ActiveSheet.Range("$A$1:$N$6398").AutoFilter Field:=10
End Sub

问题只出现在第3,4和5部分宏中。基本上,代码无法创建“好”。在状态列中过滤,即使它在代码中指定这样做。任何建议将不胜感激!

1 个答案:

答案 0 :(得分:0)

这是一个非常模糊的问题,但是如果我明白你正在尝试在列上应用过滤器,那么#34; OK&#34;然后我用以下作为基础。

ActiveSheet.Range("$A$1:$A$1000").AutoFilter Field:=1, Criteria1:="OK" 
'Change the range based on the column & num of rows in the column, this is set for column A

希望这有助于回答你的问题。