我们在多条件求和时,由于条件不定,想组和条件为dic 的key,我想达到的目的是,任意输入标题,查找到标题所在列,再循环数据,把所在的列组合为dic 的 key ,再进行求和或计数,
今天自定义一个函数,以后有用
输入【单位】【级】
输入【单位】【班】
输入【单位】【级】【班】
【代码】
Sub 多条件求和()
Dim myR As range, brr()
With Worksheets("VBA多条件求和")
arr = .range("A1").CurrentRegion
arr1 = Application.Index(arr, 1, 0)
Set myR = .range("O1").CurrentRegion
brr = StrToArray(arr1, myR)
For i = 2 To UBound(arr)
sjoin = Join(Application.Index(arr, i, brr), "")
Debug.Print sjoin
Next i
End With
End Sub
Function StrToArray(inarr, rng)
Dim rr As range, t_n%, t_m%, t_Array()
ReDim t_Array(1 To rng.Count)
On Error Resume Next
t_n = 1
For Each rr In rng
t_m = Application.WorksheetFunction.Match(rr.Value, inarr, 0)
If Err = 0 Then
t_Array(t_n) = t_m
t_n = t_n + 1
Else
MsgBox "有数据不对"
StrToArray = Array(False, False)
Exit Function
End If
Next
StrToArray = t_Array
On Error GoTo 0
End Function