我有一个数组,包含一些数据并对一行进行建模。我想检查这个“行”是否已存在于工作表中,而特定列是否符合某些条件。
例如,“生日表”包含以下数据
我有以下数组:
Dim arr(3) 'array modelling a row of the sheet
arr(0) = "Stellina" 'Name
arr(1) = 4 'Age
arr(2) = "F" 'Gender
arr(3) = #09/06/2012# 'Birthday
我想检查数组中的数据是否已经存在于工作表中。但我想只分析今天和五天前生日值相等的行。这只是为了避免分析所有行。
我有点担心比较数组和范围。有没有办法做到这一点?
答案 0 :(得分:2)
尝试通过vba应用多标准工作表函数。
debug.print cbool(application.countifs(range("a:a"), arr(0), range("b:b"), arr(1), range("c:c"), arr(2), range("d:d"), arr(3)))
答案 1 :(得分:0)
使用Filter
方法
只是为了演示使用Filter
方法的另一种方法(应用于redimmed数据阵列),正如您在OP中提到的那样:
“但我只想分析今天和五天前生日值相等的行。”
代码示例
Sub chkDates()
Dim arr(3) 'array modelling a row of the sheet
arr(0) = "Lucy" 'Name
arr(1) = 4 'Age
arr(2) = "F" 'Gender
arr(3) = #6/13/2018# 'Birthday
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Birthdays") ' << change to your sheet name
Dim n As Long, i As Long, v, vx()
' [1] get lastrow
n = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row
' [2] assign data to variant datafieldarray (2-dim, 1-based)
v = ws.Range("A1:D" & n)
' [3] rearrange data in 1-dim (!) array to allow Filter method
ReDim vx(1 To n)
For i = 2 To n
If v(i, 4) > (Now - 6) And v(i,4) <= Now Then ' get only birthdays within last 5 days
vx(i) = Join(Application.Index(v, i, 0), ",") ' join column data with colon delimiter
End If
Next i
' [4] get findings via Filter method
Debug.Print "Filter string: " & Join(arr, ","), "Findings: " & UBound(Filter(vx, Join(arr, ","), True, vbTextCompare)) + 1
End Sub