通过索引vba引用不同工作表中的一系列单元格

时间:2017-12-12 22:00:18

标签: vba excel-vba indexing excel

在excel中,我有(包含许多分类变量列的表):

A   E
S   E
D   YU
R   T
FD  R
FD  RT
D   YU
S   T
AS  R
D   RR
D   R

我想要(每个单独术语的频率):

A   1   E   2
S   2   YU  2
D   4   T   2
R   1   R   3
FD  2   RT  1
AS  1   RR  1

我正在计算每个学期的频率。我可以手动单独计算:

Sub ff()
    Range("B1:B6").Select
    Selection.FormulaArray = "=COUNTIFS(Sheet1!C[-1],Sheet2!RC[-1]:R[5]C[-1])"
    Range("D1").Select

End Sub

但是我想遍历每一列并将频率计数放在另一张表上。到目前为止,我有:

Sub Macro1()
    Sheets("Sheet1").Select
    lcol = Cells(1, Columns.Count).End(xlToLeft).Column
    N = Cells(Rows.Count, 1).End(xlUp).Row

    For j = 1 To lcol
        Range(Cells(1, j), Cells(N, j)).Select
        Selection.Copy
        Sheets("Sheet2").Select
        Cells(1, 2 * j - 1).Select
        ActiveSheet.Paste

        Columns(2 * j - 1).RemoveDuplicates Columns:=1, Header:=xlNo
        nr = Cells(Rows.Count, 2 * j - 1).End(xlUp).Row
        Range(Cells(1, 2 * j), Cells(nr, 2 * j)).Select
       *Selection.FormulaArray = "=COUNTIFS(Sheet1!C[" & j & "],Sheet2!RC[-1]:R[5]C[-1])"
        Sheets("Sheet1").Select
    Next j

End Sub
  • 是我不断收到错误的地方。如何通过索引在公式表示法中引用另一个工作表范围的单元格。我想用j来引用正确的列。

我遵循Refereing to a range of cell in another sheet的建议,但他们引用了明确的细胞。在我的例子中,我将引用显式单元格,但是通过索引引用(j)。

请注意

Sheets(2).Range("F15:AK46").Select

也给了我一个错误

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,使用字典是最好的方法。所以你的代码如下。我不知道你希望如何打印或使用你的结果,但是对于这个例子,代码将创建一个名为“Results”的表格,如果它存在,它将清除它并写下新的结果:

Option Explicit
Sub CountOccurrences()
    Dim i As Long
    Dim j As Long
    Dim cnt As Long
    Dim lCol As Integer
    Dim N As Long
    Dim key As Variant
    Dim dict As Object
    Dim WS As Worksheet
    Dim WS_Result As Worksheet

    'Set objects and create a sheet to print results
    Set WS = ThisWorkbook.Worksheets("Sheet1") 'change the name of the sheet to whatever you have

    On Error GoTo Handler
    Set WS_Result = ThisWorkbook.Worksheets("Results")
    On Error GoTo 0

    WS_Result.Cells.Clear

    'Count the columns
    lCol = WS.Cells(1, WS.Columns.Count).End(xlToLeft).Column

    'Loop thru all columns and count occurrences
    For j = 1 To lCol
        'Find the last row
        N = WS.Cells(WS.Rows.Count, lCol).End(xlUp).Row

        'Create a new dictionary
        Set dict = CreateObject("scripting.dictionary")

        For i = 1 To N
            key = WS.Cells(i, j).Value
            If dict.exists(key) = False Then
                dict.Add key, 1 'key=cell value, item=count of that value
            Else
                dict(key) = dict(key) + 1
            End If
        Next i

        'Print the results
        cnt = 0
        For Each key In dict.keys
            cnt = cnt + 1
            WS_Result.Cells(cnt, (j - 1) * 2 + 1).Value = key
            WS_Result.Cells(cnt, (j - 1) * 2 + 2).Value = dict(key)
        Next key

        'Destroy the dictionary to startover
        Set dict = Nothing
    Next j

    Exit Sub
Handler:
    Set WS_Result = ThisWorkbook.Worksheets.Add
    WS_Result.Name = "Results"
    Resume
End Sub

结果快照

enter image description here