PDA

View Full Version : Capture names from 2 arrays and create three new arrays



chapster21
10-16-2008, 05:59 PM
I have two lists of names. I have to write a sub that captures the 2 lists in 2 arrays. Then create 3 arrays from that info. 1 new array is for names of the previous year, one is for names of the current year, and the 3rd for names that are in both lists. It does not run correctly. Any ideas?


\code

Option Explicit
Option Base 1
Sub CustList()
Dim i1, i2, i3, i4, i5 As Integer
Dim size1, size2, size3, size4, size5 As Integer
Dim list1(), list2(), list3(), list4(), list5() As String
Dim index1, index2 As Integer
Dim name1, name2 As String
With Range("D3")
Range(.Offset(1, 0), .Offset(1, 3).End(xlDown)).ClearContents
End With

With Range("A3")
size1 = Range(.Offset(1, 0), .End(xlDown)).Rows.Count
ReDim list1(size1)
For i1 = 1 To size1
list1(i1) = .Offset(i1, 0)
Next
size2 = Range(.Offset(1, 1), .Offset(0, 1).End(xlDown)).Rows.Count
ReDim list2(size2)
For i2 = 1 To size2
list2(i2) = .Offset(i2, 1)
Next
End With

size3 = 0
size4 = 0
size5 = 0
index1 = 1
index2 = 1
name1 = list1(index1)
name2 = list2(index2)

Do While index1 <= size1 And index2 <= size2
name1 = list1(index1)
name2 = list2(index2)
size3 = size3 + 1
size4 = size4 + 1
size5 = size5 + 1
ReDim Preserve list3(size3), list4(size4), list5(size5)

If name1 <> name2 Then
list3(size3) = name1
index1 = index1 + 1
ElseIf name1 = name2 Then
list5(size5) = name2
'index1 = index1 + 1
index2 = index2 + 1
End If
Loop

If index1 > size1 And index2 <= size2 Then
For i2 = index2 To size2
size4 = size4 + 1
ReDim Preserve list4(size4)
list4(size4) = list2(i2)
Next
ElseIf index1 <= size1 And index2 > size2 Then
For i1 = index1 To size1
size3 = size3 + 1
ReDim Preserve list3(size3)
list3(size3) = list1(i1)
Next
End If

With Range("D3")
For i3 = 1 To size3
.Offset(i3, 0) = list3(i3)
Next
End With

With Range("E3")
For i4 = 1 To size4
.Offset(i4, 0) = list4(i4)
Next
End With

With Range("F3")
For i5 = 1 To size5
.Offset(i5, 0) = list5(i5)
Next
End With

End Sub

code/

chapster21
10-16-2008, 06:36 PM
I know it's a mess and all wrong. I am at that hair pulling out stage.