Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
dj dj is offline
external usenet poster
 
Posts: 92
Default Find / FindNext Trouble

I'm having trouble with Find and FindNext (I think). I have 2 sheets with
rows of data containing ID1, ID2, and other data (in that column order). I
want to find the row on Sheet2 that contains the same ID1 and ID2 as in a
single row on Sheet1, and mark (in a column to the left) on Sheet1 if ID1 and
ID2 is found, or just ID1, or ID1 is not found on Sheet2.

The code below correctly marks rows as "Found" (i.e., both ID1 and ID2 are
found) and "NOT FOUND: ID1". But "NOT FOUND: ID2" (which should be when ID1
is only found without the proper ID2) is reported whenever there is more than
one row with ID1, even when one of the rows has the same ID2.

Worksheets("Sheet1").Range("ID1_on_Sheet1").Select
For Each Item In Selection
With Worksheets("Sheet2").Range(ID1_on_Sheet2)
Set ID1_Cell = .Find(Item.Value, LookIn:=xlValues, LookAt:=xlWhole,
SearchOrder:=xlByRows)
If Not ID1_Cell Is Nothing Then
firstAddress = ID1_Cell.Address
Do
If Item.Offset(0, 1).Value = ID1_Cell.Offset(0, 1).Value
Then 'Compares ID2
Item.Offset(0, -1).Value = "Found"
Else: Item.Offset(0, -1).Value = "NOT FOUND: ID2"
End If
Set ID1_Cell = .FindNext(ID1_Cell)
Loop While ID1_Cell.Address < firstAddress
End If
If ID1_Cell Is Nothing Then Item.Offset(0, -1).Value = "NOT FOUND:
ID1"
End With
Next Item

Any help is much appreciated!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default Find / FindNext Trouble

You have one line in the wrong place and an extra "Not Found". You only need
the "Not found" with the Find and not with the "find Next".

Worksheets("Sheet1").Range("ID1_on_Sheet1").Select
For Each Item In Selection
With Worksheets("Sheet2").Range(ID1_on_Sheet2)
Set ID1_Cell = .Find(Item.Value, LookIn:=xlValues, LookAt:=xlWhole,
SearchOrder:=xlByRows)
If Not ID1_Cell Is Nothing Then
firstAddress = ID1_Cell.Address
Do
If Item.Offset(0, 1).Value = ID1_Cell.Offset(0, 1).Value
Then 'Compares ID2
Item.Offset(0, -1).Value = "Found"
End If
Set ID1_Cell = .FindNext(ID1_Cell)
Loop While ID1_Cell.Address < firstAddress
else
Item.Offset(0, -1).Value = "NOT FOUND:
End If
ID1"
End With
Next Item


"DJ" wrote:

I'm having trouble with Find and FindNext (I think). I have 2 sheets with
rows of data containing ID1, ID2, and other data (in that column order). I
want to find the row on Sheet2 that contains the same ID1 and ID2 as in a
single row on Sheet1, and mark (in a column to the left) on Sheet1 if ID1 and
ID2 is found, or just ID1, or ID1 is not found on Sheet2.

The code below correctly marks rows as "Found" (i.e., both ID1 and ID2 are
found) and "NOT FOUND: ID1". But "NOT FOUND: ID2" (which should be when ID1
is only found without the proper ID2) is reported whenever there is more than
one row with ID1, even when one of the rows has the same ID2.

Worksheets("Sheet1").Range("ID1_on_Sheet1").Select
For Each Item In Selection
With Worksheets("Sheet2").Range(ID1_on_Sheet2)
Set ID1_Cell = .Find(Item.Value, LookIn:=xlValues, LookAt:=xlWhole,
SearchOrder:=xlByRows)
If Not ID1_Cell Is Nothing Then
firstAddress = ID1_Cell.Address
Do
If Item.Offset(0, 1).Value = ID1_Cell.Offset(0, 1).Value
Then 'Compares ID2
Item.Offset(0, -1).Value = "Found"
Else: Item.Offset(0, -1).Value = "NOT FOUND: ID2"
End If
Set ID1_Cell = .FindNext(ID1_Cell)
Loop While ID1_Cell.Address < firstAddress
End If
If ID1_Cell Is Nothing Then Item.Offset(0, -1).Value = "NOT FOUND:
ID1"
End With
Next Item

Any help is much appreciated!

  #3   Report Post  
Posted to microsoft.public.excel.programming
dj dj is offline
external usenet poster
 
Posts: 92
Default Find / FindNext Trouble

Joel,
Thanks for your help!

DJ

"Joel" wrote:

You have one line in the wrong place and an extra "Not Found". You only need
the "Not found" with the Find and not with the "find Next".

Worksheets("Sheet1").Range("ID1_on_Sheet1").Select
For Each Item In Selection
With Worksheets("Sheet2").Range(ID1_on_Sheet2)
Set ID1_Cell = .Find(Item.Value, LookIn:=xlValues, LookAt:=xlWhole,
SearchOrder:=xlByRows)
If Not ID1_Cell Is Nothing Then
firstAddress = ID1_Cell.Address
Do
If Item.Offset(0, 1).Value = ID1_Cell.Offset(0, 1).Value
Then 'Compares ID2
Item.Offset(0, -1).Value = "Found"
End If
Set ID1_Cell = .FindNext(ID1_Cell)
Loop While ID1_Cell.Address < firstAddress
else
Item.Offset(0, -1).Value = "NOT FOUND:
End If
ID1"
End With
Next Item


"DJ" wrote:

I'm having trouble with Find and FindNext (I think). I have 2 sheets with
rows of data containing ID1, ID2, and other data (in that column order). I
want to find the row on Sheet2 that contains the same ID1 and ID2 as in a
single row on Sheet1, and mark (in a column to the left) on Sheet1 if ID1 and
ID2 is found, or just ID1, or ID1 is not found on Sheet2.

The code below correctly marks rows as "Found" (i.e., both ID1 and ID2 are
found) and "NOT FOUND: ID1". But "NOT FOUND: ID2" (which should be when ID1
is only found without the proper ID2) is reported whenever there is more than
one row with ID1, even when one of the rows has the same ID2.

Worksheets("Sheet1").Range("ID1_on_Sheet1").Select
For Each Item In Selection
With Worksheets("Sheet2").Range(ID1_on_Sheet2)
Set ID1_Cell = .Find(Item.Value, LookIn:=xlValues, LookAt:=xlWhole,
SearchOrder:=xlByRows)
If Not ID1_Cell Is Nothing Then
firstAddress = ID1_Cell.Address
Do
If Item.Offset(0, 1).Value = ID1_Cell.Offset(0, 1).Value
Then 'Compares ID2
Item.Offset(0, -1).Value = "Found"
Else: Item.Offset(0, -1).Value = "NOT FOUND: ID2"
End If
Set ID1_Cell = .FindNext(ID1_Cell)
Loop While ID1_Cell.Address < firstAddress
End If
If ID1_Cell Is Nothing Then Item.Offset(0, -1).Value = "NOT FOUND:
ID1"
End With
Next Item

Any help is much appreciated!

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find / findnext question Gary Keramidas Excel Programming 2 February 1st 08 07:18 PM
Using Find & FindNext in a form BernzG[_13_] Excel Programming 3 August 19th 05 12:28 AM
Using 'Find' and 'FindNext' in vba SA3214 Excel Programming 3 March 25th 05 12:17 PM
Find, Findnext VBA Loop SMS - John Howard Excel Programming 5 November 13th 04 03:19 AM
Find...FindNext Problem mtsark Excel Programming 4 August 19th 04 04:09 PM


All times are GMT +1. The time now is 02:04 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"