Excel VBA - 循环遍历多个数据变量的简单方法

时间:2017-12-19 15:57:54

标签: excel vba excel-vba

有趣的问题除了" no"之外可能没有答案。我有以下代码填写电子表格。解释这一行:

CACsatSummary.Range("B2") = WorksheetFunction.CountIfs(name, var01, score, "1")

等于左边是明显的(工作表和范围)。右边是CountIfs函数。接下来是" name",这是C列的范围.Var01-Var07是列C中存在的7个分析师名称(dim' d并且高于我提供的值)#&# 34;得分"是另一个范围,这一列是D列。" 1" (也是,2,3,4和5)是D列中存在的值。

整个代码所做的是循环遍历数据表,并计算每个分析员(var01 - var07)的每个分数(1-5)的实例数,并将它们放入一个新表中,该表总结了所有数据。虽然下面的代码工作正常,但我觉得可能有更好的方法来编写它,而不是当前的40多行。

感谢任何帮助。

'Fill in data
    Dim var01, var02, var03, var04, var05, var06, var07 As String
    var01 = "A,  Person"
    var02 = "B,  Person"
    var03 = "C,  Person"
    var04 = "D,  Person"
    var05 = "E,  Person"
    var06 = "F,  Person"
    var07 = "G,  Person"

    Dim CACsatSummary As Worksheet, CACsatDetails As Worksheet, name As Range, score As Range
    Set CACsatSummary = Sheets("CSAT Summary")
    Set CACsatDetails = Sheets("CSAT Details")
    Set name = Sheets("CSAT Details").Range("C2:C10000")
    Set score = Sheets("CSAT Details").Range("D2:D10000")

'Find the values
    CACsatSummary.Range("B2") = WorksheetFunction.CountIfs(name, var01, score, "1")
    CACsatSummary.Range("B3") = WorksheetFunction.CountIfs(name, var02, score, "1")
    CACsatSummary.Range("B4") = WorksheetFunction.CountIfs(name, var03, score, "1")
    CACsatSummary.Range("B5") = WorksheetFunction.CountIfs(name, var04, score, "1")
    CACsatSummary.Range("B6") = WorksheetFunction.CountIfs(name, var05, score, "1")
    CACsatSummary.Range("B7") = WorksheetFunction.CountIfs(name, var06, score, "1")
    CACsatSummary.Range("B8") = WorksheetFunction.CountIfs(name, var07, score, "1")
    CACsatSummary.Range("C2") = WorksheetFunction.CountIfs(name, var01, score, "2")
    CACsatSummary.Range("C3") = WorksheetFunction.CountIfs(name, var02, score, "2")
    CACsatSummary.Range("C4") = WorksheetFunction.CountIfs(name, var03, score, "2")
    CACsatSummary.Range("C5") = WorksheetFunction.CountIfs(name, var04, score, "2")
    CACsatSummary.Range("C6") = WorksheetFunction.CountIfs(name, var05, score, "2")
    CACsatSummary.Range("C7") = WorksheetFunction.CountIfs(name, var06, score, "2")
    CACsatSummary.Range("C8") = WorksheetFunction.CountIfs(name, var07, score, "2")
    CACsatSummary.Range("D2") = WorksheetFunction.CountIfs(name, var01, score, "3")
    CACsatSummary.Range("D3") = WorksheetFunction.CountIfs(name, var02, score, "3")
    CACsatSummary.Range("D4") = WorksheetFunction.CountIfs(name, var03, score, "3")
    CACsatSummary.Range("D5") = WorksheetFunction.CountIfs(name, var04, score, "3")
    CACsatSummary.Range("D6") = WorksheetFunction.CountIfs(name, var05, score, "3")
    CACsatSummary.Range("D7") = WorksheetFunction.CountIfs(name, var06, score, "3")
    CACsatSummary.Range("D8") = WorksheetFunction.CountIfs(name, var07, score, "3")
    CACsatSummary.Range("E2") = WorksheetFunction.CountIfs(name, var01, score, "4")
    CACsatSummary.Range("E3") = WorksheetFunction.CountIfs(name, var02, score, "4")
    CACsatSummary.Range("E4") = WorksheetFunction.CountIfs(name, var03, score, "4")
    CACsatSummary.Range("E5") = WorksheetFunction.CountIfs(name, var04, score, "4")
    CACsatSummary.Range("E6") = WorksheetFunction.CountIfs(name, var05, score, "4")
    CACsatSummary.Range("E7") = WorksheetFunction.CountIfs(name, var06, score, "4")
    CACsatSummary.Range("E8") = WorksheetFunction.CountIfs(name, var07, score, "4")
    CACsatSummary.Range("F2") = WorksheetFunction.CountIfs(name, var01, score, "5")
    CACsatSummary.Range("F3") = WorksheetFunction.CountIfs(name, var02, score, "5")
    CACsatSummary.Range("F4") = WorksheetFunction.CountIfs(name, var03, score, "5")
    CACsatSummary.Range("F5") = WorksheetFunction.CountIfs(name, var04, score, "5")
    CACsatSummary.Range("F6") = WorksheetFunction.CountIfs(name, var05, score, "5")
    CACsatSummary.Range("F7") = WorksheetFunction.CountIfs(name, var06, score, "5")
    CACsatSummary.Range("F8") = WorksheetFunction.CountIfs(name, var07, score, "5")

2 个答案:

答案 0 :(得分:2)

您遇到的问题是正确循环。并控制这个循环。通常,您必须遍历Excel中的列和行,并从工作表函数中为它们提供一些值,其中包含一些变量。为了方便起见,我将var01var02 ... var07写入名为myPeople的数组()中,然后我将遍历它们,将每个一个细胞。

更好看&测试比解释:

Sub TestMe()

    Dim myPeople    As Variant
    Dim cntRows     As Long
    Dim cntCols     As Long
    Dim cntArr      As Long        

    myPeople = Array("A", "B", "C", "D", "E", "F", "G")

    For cntCols = 2 To 7        'column B to G
        For cntRows = 2 To 8    'row 2 to row 8
            With Worksheets(1)
                .Cells(cntRows, cntCols) = myPeople(cntArr Mod UBound(myPeople))
            End With
            cntArr = cntArr + 1
        Next cntRows
    Next cntCols

End Sub

你的任务中最棘手的部分是找到一种循环遍历myPeople数组的方法,一旦你从头开始就开始。这可以通过Mod函数+递增的计数器轻松实现。因此,在代码结束时,您将实现此目的:

enter image description here

这不是你想要的,但它有两个主要任务的答案:

  • 如何循环细胞
  • 如何遍历数组

从那里到你的WorksheetFunction.CountIfs方式应该清楚,如果你尝试大约30分钟。

答案 1 :(得分:2)

我使用了一个选择案例来更改分析人员名称和两个for循环来循环遍历不同的行和列。您可以从Vityata的答案中换出阵列的Select Case。然后用变量i(mypeople(i))

增加数组
    Dim CACsatSummary As Worksheet, CACsatDetails As Worksheet, name As Range, score As Range
    Set CACsatSummary = Sheets("CSAT Summary")
    Set CACsatDetails = Sheets("CSAT Details")
    Set Names = Sheets("CSAT Details").Range("C2:C10000")
    Set score = Sheets("CSAT Details").Range("D2:D10000")

'Find the values
For x = 1 To 5
   For i = 1 To 7
    Select Case i
    Case 1
    varN = "A,  Person"
       Case 2
    varN = "B,  Person"
        Case 3
    varN = "C,  Person"
        Case 4
    varN = "D,  Person"
        Case 5
    varN = "E,  Person"
        Case 6
    varN = "F,  Person"
        Case 7
    varN = "G,  Person"
    End Select
CACsatSummary.Cells(i + 1, x + 1) = WorksheetFunction.CountIfs(Names, varN, score, x)
  Next i
  Next x