1

这几天我一直在为此苦苦挣扎。任何帮助将不胜感激!

这很难解释,所以我会尽力而为。

我要做的是计算每个查询的结果数,然后根据该结果数对它们进行分类。

例如,如果 Query_A 有 1 个准确结果,然后 Query_Z 有 1 个准确结果,那么总共有 2 个查询有 1 个结果。

我目前正在尝试将 Loop 与 if then 语句一起使用,但我不知所措。

这是一些示例数据和我希望的输出:Query_Example_Data_and_Results.xlsx - 这不是我真正的电子表格,因为它包含数千行数据和非常大的文件大小。

下面的代码确实提取了查询计数(删除查询重复),但没有给出查询结果计数。我会提供我的代码尝试,但我知道我什至没有接近...所以我删除了我的失败的尝试希望我足够清楚以引导正确的方向。

Sub Query_Count()

G_40 = 0

Query = ""

Application.StatusBar = " ~~ ~~ QUERY COUNT ~~ RUNNING ~~ ~~ " & x

x = 2

Do Until Sheets(1).Cells(x, 1) = ""

    If Sheets(1).Cells(x, 9) = "Yes" Then
    If Query <> Sheets(1).Cells(x, 1) Then
        G_40 = G_40 + 1
    End If
    End If
    Query = Sheets(1).Cells(x, 1)

x = x + 1

Loop

Application.StatusBar = "DONE RUNNING QUERY COUNT OF " & x & " ROWS!"

G = 40
Sheets(3).Cells(G, 7) = G_40 'query_count:

End Sub

先感谢您!

4

1 回答 1

1

根据您的示例,此代码将完成这项工作:

Option Explicit

Sub getResults()
    Application.ScreenUpdating = False

    Dim ws1 As Worksheet, ws2 As Worksheet, lr&
        Set ws1 = ThisWorkbook.Sheets("Example_Query_Data")
        Set ws2 = ThisWorkbook.Sheets("Example_Results")
        lr = ws1.Range("A" & Rows.count).End(xlUp).Row

    Dim arr() As String, i&, j&, cnt&
    Dim varr() As String
    cnt = 0

    ReDim arr(lr - 2)
    For i = 2 To lr
        arr(i - 2) = CStr(ws1.Range("A" & i).Value) ' fill array
    Next i
    Call RemoveDuplicate(arr) 'remove duplicate
    ReDim varr(0 To UBound(arr), 0 To 1)
    For i = LBound(arr) To UBound(arr)
        varr(i, 0) = arr(i)
        varr(i, 1) = getCount(arr(i), ws1, j, lr)
    Next i

    Call PrepTable(ws2)
    Call UpdateTable(ws2, ws1, varr, j, lr) ' Update table

    Application.ScreenUpdating = True
End Sub

Function getCount(qName$, ByRef ws1 As Worksheet, ByRef i&, lr&)
    Dim count&
    count = 0
    For i = 2 To lr
        If (StrComp(CStr(ws1.Range("A" & i).Value), qName, vbTextCompare) = 0) And _
              (StrComp(CStr(ws1.Range("C" & i).Value), "Yes", vbTextCompare) = 0) Then count = count + 1
    Next i
    getCount = count ' return count
End Function

Sub UpdateTable(ByRef ws As Worksheet, ByRef ws2, ByRef arr() As String, ByRef i&, lr&)
    Dim tblIter&
    For tblIter = 2 To 12
        For i = LBound(arr) To UBound(arr)
            If arr(i, 1) = tblIter - 1 Then
                ws.Range("B" & tblIter).Value = ws.Range("B" & tblIter).Value + 1
            End If
        Next i
    Next tblIter
    Call ElevenAndMore(ws, ws2, arr, lr, i)
End Sub

Sub PrepTable(ws As Worksheet)
    ws.Range("B2:B12").ClearContents
End Sub

Sub ElevenAndMore(ByRef ws As Worksheet, ByRef ws2, ByRef arr() As String, lr&, ByRef i)
    Dim cnt&, j&
    cnt = 0
    For i = LBound(arr) To UBound(arr)
     For j = 1 To lr
        If StrComp(CStr(ws2.Range("A" & j).Value), arr(i, 0), vbTextCompare) = 0 Then
            cnt = cnt + 1
        End If
     Next j
     If cnt > 10 Then ws.Range("B12").Value = ws.Range("B12").Value + 1
     cnt = 0
    Next i
End Sub

Sub RemoveDuplicate(ByRef StringArray() As String)
    Dim lowBound$, UpBound&, A&, B&, cur&, tempArray() As String
    If (Not StringArray) = True Then Exit Sub ' is empty?
    lowBound = LBound(StringArray)
    UpBound = UBound(StringArray)
    ReDim tempArray(lowBound To UpBound)
    cur = lowBound ' first item
    tempArray(cur) = StringArray(lowBound)
    For A = lowBound + 1 To UpBound
        For B = lowBound To cur
            If LenB(tempArray(B)) = LenB(StringArray(A)) Then
                If InStrB(1, StringArray(A), tempArray(B), vbBinaryCompare) = 1 Then Exit For
            End If
        Next B
        If B > cur Then cur = B: tempArray(cur) = StringArray(A)
    Next A
    ReDim Preserve tempArray(lowBound To cur) ' reSize
    StringArray = tempArray ' copy
End Sub

评论后编辑: 更改这三个:

将 +28 添加到 tblIter

Sub UpdateTable(ByRef ws As Worksheet, ByRef ws2, ByRef arr() As String, ByRef i&, lr&)
    Dim tblIter&
    For tblIter = 2 To 12
        For i = LBound(arr) To UBound(arr)
            If arr(i, 1) = tblIter - 1 Then
                ws.Range("B" & tblIter + 28).Value = ws.Range("B" & tblIter + 28).Value + 1
            End If
        Next i
    Next tblIter
    Call ElevenAndMore(ws, ws2, arr, lr, i)
End Sub

只需将位置更改为 B40

Sub ElevenAndMore(ByRef ws As Worksheet, ByRef ws2, ByRef arr() As String, lr&, ByRef i)
    Dim cnt&, j&
    cnt = 0
    For i = LBound(arr) To UBound(arr)
     For j = 1 To lr
        If StrComp(CStr(ws2.Range("A" & j).Value), arr(i, 0), vbTextCompare) = 0 Then
            cnt = cnt + 1
        End If
     Next j
     If cnt > 10 Then ws.Range("B40").Value = ws.Range("B40").Value + 1
     cnt = 0
    Next i
End Sub

和准备表变化范围

Sub PrepTable(ws As Worksheet)
    ws.Range("B30:B40").ClearContents
End Sub

这应该做!

于 2013-04-18T11:11:38.087 回答