如何过滤VBA代码中的“其他”

问题描述 投票:0回答:2

我创建了一个过滤/ countIF关键字的宏,并计算我的报告的数字,这是正常的。我需要的帮助是如何过滤我的代码中被视为“其他”的单元格。如下图所示以红色突出显示,有很多项目被计为“其他”。我需要找出它们是哪个单元格,以便我可以修改我的宏来查找这些项目。

enter image description here

Public Sub Testing()

Dim row_number As Long
Dim count_of_Harmo As Long
Dim count_of_Room As Long
Dim count_of_Skyp As Long
Dim count_of_others As Long


Dim items As Variant
Dim cursht As String 'for the macro to run in any sheet
cursht = ActiveSheet.Name 'for the macro to run in any sheet

row_number = 1
count_of_Harmo = 0
count_of_Room = 0
count_of_Skyp = 0
count_of_others = 0


Do

row_number = row_number + 1
items = Sheets(cursht).Range("N" & row_number)

    If InStr(items, "harmo") Then
        count_of_Harmo = count_of_Harmo + 1
    ElseIf InStr(items, "room") Then
        count_of_Room = count_of_Room + 1
    ElseIf InStr(items, "skyp") Or InStr(items, "meeting") Then
        count_of_Skyp = count_of_Skyp + 1
    ElseIf items <> "" Then
        count_of_others = count_of_others + 1
    End If


Loop Until items = ""


Range("N2").Select


Selection.End(xlDown).Select
lastCell = ActiveCell.Address

ActiveCell.Offset(3, 1).Value = "Count"
ActiveCell.Offset(4, 1).Value = count_of_Harmo
ActiveCell.Offset(5, 1).Value = count_of_Room
ActiveCell.Offset(6, 1).Value = count_of_Skyp
ActiveCell.Offset(7, 1).Value = count_of_others

ActiveCell.Offset(3, 0).Value = "Add-ins breakdown"
ActiveCell.Offset(4, 0).Value = "HarmonIE"
ActiveCell.Offset(5, 0).Value = "Room Finder"
ActiveCell.Offset(6, 0).Value = "Skype"
ActiveCell.Offset(7, 0).Value = "Others"


End Sub
excel vba excel-vba
2个回答
2
投票

你有两种方法可以做到这一点。第一个,也就是我个人选择的那个,就是在工作表的末尾创建一个新列来添加一个类别。这看起来像这样:

If InStr(items, "harmo") Then
    count_of_Harmo = count_of_Harmo + 1
    Range("Q" & row_number).Value ="Harmon"
ElseIf InStr(items, "room") Then
    count_of_Room = count_of_Room + 1
    Range("Q" & row_number).Value ="Room Finder"
ElseIf InStr(items, "skyp") Or InStr(items, "meeting") Then
    count_of_Skyp = count_of_Skyp + 1
    Range("Q" & row_number).Value ="Skype"
ElseIf items <> "" Then
    count_of_others = count_of_others + 1
    Range("Q" & row_number).Value ="Other"
End If

当然,你可以将列打开。然后,当宏运行完毕后,您可以按该列过滤以查找电子表格中的所有其他内容。

另一种方式,而不是添加类别,将隐藏行,如果它们是除了其他之外的任何东西。


1
投票

最好使用countif功能。

Sub test()
    Dim rngDB As Range, rngT As Range
    Dim vR(1 To 5, 1 To 2)

    Set rngDB = Range("n2", Range("n2").End(xlDown))
    Set rngT = Range("n2").End(xlDown)(4)

    With WorksheetFunction
        vR(1, 1) = "Add-ins breakdown"
        vR(2, 1) = "HarmonIE"
        vR(3, 1) = "Room Finder"
        vR(4, 1) = "Skype"
        vR(5, 1) = "Others"

        vR(1, 2) = "count"
        vR(2, 2) = .CountIf(rngDB, "*harmo*")
        vR(3, 2) = .CountIf(rngDB, "*room*")
        vR(4, 2) = .CountIf(rngDB, "*skyp*") + .CountIf(rngDB, "*meeting*") - .CountIf(rngDB, "*skyp*meeting*")
        vR(5, 2) = .CountA(rngDB) - vR(2, 2) - vR(3, 2) - vR(4, 2)
    End With
    rngT.CurrentRegion.Clear
    rngT.Resize(5, 2) = vR
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.