Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算Excel VBA中另一个值的同一行中的关联值_Excel_Vba - Fatal编程技术网

计算Excel VBA中另一个值的同一行中的关联值

计算Excel VBA中另一个值的同一行中的关联值,excel,vba,Excel,Vba,我在计算与标识值位于同一行的值时遇到问题。该表应显示与下图所示相同的精确值。生成一个表,显示这些值相互关联的次数。到目前为止,我的代码非常混乱,我不太擅长excel vba: Sub Button1_Click() Dim cell As Range, f As Range Dim rowOffset As Long With Worksheets("Sheet1").Range("A3:F2000") '<--| change this to your actual range of

我在计算与标识值位于同一行的值时遇到问题。该表应显示与下图所示相同的精确值。生成一个表,显示这些值相互关联的次数。到目前为止,我的代码非常混乱,我不太擅长excel vba:

Sub Button1_Click()  Dim cell As Range, f As Range
Dim rowOffset As Long

With Worksheets("Sheet1").Range("A3:F2000") '<--| change this to your actual range of interest

    For Each cell In .SpecialCells(xlCellTypeConstants, xlNumbers)

        rowOffset = 0 '<--| rowOffset was once set to 1

        Set f = .Find(what:=cell, after:=cell, LookIn:=xlValues, lookat:=xlWhole, searchdirection:=xlPrevious)

        If Not f Is Nothing And f.Row <= cell.Row Then rowOffset = cell.Row - f.Row '<--| if Data has occurred is assumed, add + 1 after this line of codes here.

        Worksheets("gapByRow").Cells(cell.Row, cell.Column).Value = rowOffset

    Next cell
End With End Sub
子按钮1\u Click()将单元格设置为范围,将f设置为范围
将行偏移设置为等长

使用工作表(“Sheet1”).Range(“A3:F2000”)”而不是按钮,我会将其设置为自定义项,然后可以像公式一样调用它:

Function MyCount(Rng As Range, xVal, Yval)
Application.Volatile
Dim rngArr() As Variant
rngArr = Rng.Value
For i = 1 To UBound(rngArr, 1)
    For j = 1 To UBound(rngArr, 2)
        If rngArr(i, j) = xVal Then
            For k = 1 To UBound(rngArr, 2)
                If rngArr(i, k) = Yval Then MyCount = MyCount + 1
            Next k
        End If
    Next j
Next i

End Function
您可以将其放在I3中:

=MyCount($A$3:$F$17,$H3,I$2)
然后在矩阵上来回复制


然后我会制作计算手册,在数据更新后点击F9。

您使用VBA有什么原因吗?这可以用
CountIf
公式的矩阵来完成?我有一种似曾相识的感觉-我确信这个问题在过去一两周里被问了大约六次-而且不总是由同一个人提出。我不知道你是怎么做到的,但它是伟大的人!!!!你最好!!我努力给你的答案打分。。。。泰。。