You have small ranges, so you can use Cells:
Sub Count_Names2()
Dim vaNames() As Variant, vaPNames() As Variant
Dim i As Long, vaCount As Long, r As Range
vaNames = Sheets("Name_Matrix").Range("B5:C68").Value
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 1 To UBound(vaNames)
If vaNames(i, 1) = "P" Then .Item(vaNames(i, 2)) = 1 '(just fill the dictionary)
Next i
For Each r In Range("C5:D14,H5:I15").Cells
If Len(r.Value) > 0 Then
If .Exists(r.Value) Then 'If value in array matches value in 2nd array
vaCount = vaCount + 1: ReDim Preserve vaPNames(1 To vaCount) '(3rd array is one-dimensional array)
vaPNames(vaCount) = r.Value '...I want to add that value to a 3rd array.
End If
End If
Next r
End With
MsgBox UBound(vaPNames)
End Sub
If you want to use arrays, it will look something like this (just for testing):
Sub test()
Dim x, y, a, ResultArr(), i&, j&, k& 'j& and j As Long are same
x = Range("C5:D14").Value: y = Range("H5:I15").Value
ReDim ResultArr(1 To UBound(x) + UBound(y)) 'UBound(x, 1) and UBound(x) are same
For Each a In x
If Len(a) > 0 Then k = k + 1: ResultArr(k) = a
Next a
For i = 1 To UBound(y) 'or For Each a In y - in this case are same
For j = 1 To UBound(y, 2)
If Not IsEmpty(y(i, j)) Then k = k + 1: ResultArr(k) = y(i, j)
Next j
Next i
MsgBox "UBound(ResultArr) = " & UBound(ResultArr)
ReDim Preserve ResultArr(1 To k)
MsgBox "and now UBound(ResultArr) = " & UBound(ResultArr)
MsgBox "the 2nd element is """ & ResultArr(2) & """"
'and maybe:
'Me.lstPNames.List = ResultArr
End Sub
Bookmarks