I really appreciate any help I can get on this.
I'm trying to loop through a column looking for duplicate names then taking that and several of other data from same row and placing them into an 2D array that I want to use another function, but it's not working.
I really need your help figuring out why I cannot redim this array without preserving the data.
Dim oRange As Range, aCell As Range, bCell As Range
Dim ws As Worksheet
Dim SearchString As String, FoundAt As String
Dim tArray() As Variant
Dim iR As Long
Dim LastRow As Long
Dim LastCol As Long
'name of the worksheet
Set ws = Worksheets("VML Daily")
'column 6 has a huge list of names
Set oRange = ws.Columns(6)
'the keyword (there are 7 'ABC Company 1' in the column above)
SearchString = "ABC Company 1"
'Find keyword in column
Set aCell = oRange.Find(What:=SearchString, LookIn:=xlValues, _
LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
'find last row and column number
LastRow = Range("A1").End(xlDown).Row
'redimensioning based on maximum rows
ReDim Preserve tArray(1 To LastRow, 1 To 3) As Variant
'if search finds something
If Not aCell Is Nothing Then
Set bCell = aCell
FoundAt = aCell.Address
iR = 1
tArray(1, 1) = aCell
tArray(1, 2) = aCell.Offset(0, 33)
tArray(1, 3) = aCell.Offset(0, 38)
'continue finding stuff until end
Do
Set aCell = oRange.FindNext(After:=aCell)
If Not aCell Is Nothing Then
If aCell.Address = bCell.Address Then Exit Do
FoundAt = FoundAt & ", " & aCell.Address
tArray(iR, 1) = aCell
tArray(iR, 2) = aCell.Offset(0, 33)
tArray(iR, 3) = aCell.Offset(0, 38)
iR = iR + 1
Else
Exit Do
End If
Loop
'redim'ing the array to the amount of hits I found above and preserve the data
'Here's where it error's out as "Subscript out of range"
ReDim Preserve tArray(1 To iR, 1 To 3) As Variant
Else
MsgBox SearchString & " not Found"
Exit Sub
End If