我在计算与识别值在同一行中的值时遇到问题。该表应显示与下图相同的精确值。生成一个表,显示这些值彼此关联的次数。继续我到目前为止,我的代码非常混乱,而且我并不擅长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
下面的示例有一个表格,显示值关联的时间。具有黄色背景的单元格是“已识别的值”,具有绿色的单元格是关联的。正如你所看到的,黄色背景上的8旁边有值为5,1,0,0,3,2,5,7等等。这意味着8与1相关联5次,2合1次, 3 in 0,4 in 0,5 in 3,6 in in 2依此类推。如果vba代码是正确的,它应该在这里显示相同的结果。
答案 0 :(得分:1)
而不是按钮我会把它变成UDF,然后可以像公式一样调用它:
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。