G
Geoff
Hi,
I have 4 columns, A and B are new customers and
locations, E and F are archived customers and locations.
I need to add new customer details to the archived list
at the end, prior to a final sort.
The following code works fine if the customer name did
not exist previously but fails otherwise.
Also because I sort both lists before doing the search,
is there a quicker way of continuing the search if
nothing is found, see comment in code?
I would be grateful if someone could correct the code or
for any advice.
T.I.A
Geoff
Code:
Sub aaaa()
Dim rng As Range, rng1 As Range, rng3 As Range
Dim cust As Range, i As Integer, firstFind As String
Set rng = Range("A1")
Set cust = Range("E1")
With Sheets(1)
Set rng1 = .Range(.Range("A1"), .Range("A1:A" & .Range _
("A65536").End(xlUp).Row))
Set rng3 = .Range(.Range("E1"), .Range("E1:E" & .Range _
("E65536").End(xlUp).Row))
i = 0
For Each rng In rng1
Set cust = rng3.Find(what:=rng, _
After:=cust, _
SearchDirection:=xlNext)
firstFind = ""
If Not cust Is Nothing Then
Do Until rng.Offset(0, 1) = cust.Offset(0, 1)
Or cust.Address = firstFind
If firstFind = "" Then firstFind =
cust.Address
Set cust = rng3.FindNext(After:=cust)
i = i + 1
Loop
Else
.Range("E" & rng3.Rows.Count + i) = rng
.Range("F" & rng3.Rows.Count + i) = rng.Offset
(0, 1)
i = i + 1
Set cust = Range("E1") 'Do I have to start
at the beginning if the list is sorted??
End If
Next rng
End With
End Sub
I have 4 columns, A and B are new customers and
locations, E and F are archived customers and locations.
I need to add new customer details to the archived list
at the end, prior to a final sort.
The following code works fine if the customer name did
not exist previously but fails otherwise.
Also because I sort both lists before doing the search,
is there a quicker way of continuing the search if
nothing is found, see comment in code?
I would be grateful if someone could correct the code or
for any advice.
T.I.A
Geoff
Code:
Sub aaaa()
Dim rng As Range, rng1 As Range, rng3 As Range
Dim cust As Range, i As Integer, firstFind As String
Set rng = Range("A1")
Set cust = Range("E1")
With Sheets(1)
Set rng1 = .Range(.Range("A1"), .Range("A1:A" & .Range _
("A65536").End(xlUp).Row))
Set rng3 = .Range(.Range("E1"), .Range("E1:E" & .Range _
("E65536").End(xlUp).Row))
i = 0
For Each rng In rng1
Set cust = rng3.Find(what:=rng, _
After:=cust, _
SearchDirection:=xlNext)
firstFind = ""
If Not cust Is Nothing Then
Do Until rng.Offset(0, 1) = cust.Offset(0, 1)
Or cust.Address = firstFind
If firstFind = "" Then firstFind =
cust.Address
Set cust = rng3.FindNext(After:=cust)
i = i + 1
Loop
Else
.Range("E" & rng3.Rows.Count + i) = rng
.Range("F" & rng3.Rows.Count + i) = rng.Offset
(0, 1)
i = i + 1
Set cust = Range("E1") 'Do I have to start
at the beginning if the list is sorted??
End If
Next rng
End With
End Sub