过滤列中的数据,然后计数

时间:2014-06-04 19:03:28

标签: excel vba excel-vba excel-2013

我录制了一个宏来更改工作表。基本上它会做一些更改,例如添加一列,移动两列等等。我感到困惑的是添加一个小代码,分别给出MO REAL列L中总DL和IDL的计数,并将总计数放在单元格B17和C17中同一工作簿“Resultados”中的另一个工作表上......关于如何实现这一点的任何想法?这是录制的代码:

Option Explicit
Sub DefineDL_IDL()

Dim wbTHMacro As Workbook, wsRegulares As Worksheet, wsRegularesDemitidos As Worksheet, wsTempActivos As Worksheet, _
wsTempJA As Worksheet, wsTempFit As Worksheet, wsTempDemitidos As Worksheet, wsPS As Worksheet, wsResultados As Worksheet, _
wsDLList As Worksheet, wssheet As Worksheet

Sheets("Regulares").Select

Columns("J:J").Select
    Selection.Insert Shift:=xlToRight
    Range("J1").Select
    ActiveCell.FormulaR1C1 = "MO REAL"
    Columns("K:K").Select
    Selection.Cut
    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight

    Columns("Q:Q").Select
    Selection.Cut

    Columns("I:I").Select
    Selection.Insert Shift:=xlToRight
    Range("K1").Select
    Selection.AutoFilter

    ActiveSheet.Range("A:Z").AutoFilter Field:=11, Criteria1:= _
        "INATIVE"
    Rows("5:5").Select
    Range("F5").Activate
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Delete Shift:=xlUp
    ActiveSheet.Range("A:Z").AutoFilter Field:=11
    Range("L2").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],'DL List'!RC[-11]:R[32]C[-10],2,0)"
    Range("L2").Select
    Selection.AutoFill Destination:=Range("L2:L5890")
    Range("L2:L5890").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False
    Range("L2").Select
    ActiveSheet.Range("A:Z").AutoFilter Field:=11, Criteria1:="DL"
    ActiveSheet.Range("A:Z").AutoFilter Field:=12, Criteria1:="#N/A"
    Range("L23").Select
    ActiveCell.FormulaR1C1 = "DL"
    Range("L23").Select
    Selection.Copy
    Range("L25").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    Application.CutCopyMode = False
    ActiveSheet.Range("A:Z").AutoFilter Field:=12
    Range("L4").Select
    ActiveSheet.Range("A:Z").AutoFilter Field:=11, Criteria1:=Array( _
        "G&A", "MOH", "IDL", "Other MOH"), Operator:=xlFilterValues
    ActiveSheet.Range("A:Z").AutoFilter Field:=12, Criteria1:="#N/A"
    Range("L7").Select
    ActiveCell.FormulaR1C1 = "IDL"
    Range("L7").Select
    Selection.Copy
    Range("L15").Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.Paste
    ActiveSheet.Paste
    Application.CutCopyMode = False
    Range("L7").Select
    ActiveWorkbook.Worksheets("Regulares").AutoFilter.Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("Regulares").AutoFilter.Sort.SortFields.Add Key:= _
        Range("K1:K5890"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
        :=xlSortNormal
    ActiveSheet.Range("A:Z").AutoFilter Field:=12
    Range("K2").Select
    ActiveSheet.Range("A:Z").AutoFilter Field:=11
    Range("G2").Select
    ActiveCell.FormulaR1C1 = "1"
    Range("G2").Select
    Selection.Copy
    Range("J2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.PasteSpecial Paste:=xlPasteAll, Operation:=xlMultiply, _
        SkipBlanks:=False, Transpose:=False
    Range("J2").Select
    Application.CutCopyMode = False

End Sub

2 个答案:

答案 0 :(得分:1)

如果不了解你正在做的事情的更广泛的情况,你的问题会有点混乱。您需要做的就是计算并将其放在另一张表中:

  

表格(“AnotherSheet”)。范围(“B13”)= Application.WorksheetFunction.CountA(列(“G:G”))

答案 1 :(得分:1)

如果你正在计算时间" DL"和" IDL"发生...

'计算DL和IDL

count_DL = Application.WorksheetFunction.CountIf(ActiveSheet.Range("L:L"), "DL")

count_IDL = Application.WorksheetFunction.CountIf(ActiveSheet.Range("L:L"), "IDL")

'将结果粘贴到Resultados表

Worksheets("Resultados").Range("B17") = count_DL

Worksheets("Resultados").Range("C17") = count_IDL