Help with Do While Loop

Adrae

Active Member
Joined
Feb 19, 2002
Messages
306
the following code should keep looking until FirstValue does not equal SecondValue or until the cell in column A of sheet2 is empty.

It isn't working. I'm a bit of a novice and the book I have is not helping to any great extent.

Anything you can suggest is greatly appreciated.

Sub ProductServiceBoth()
Dim FirstValue, SecondValue As String
Dim Cel As Range
For Each Cel In Selection
Set c = Worksheets("Sheet2").Columns("A:A").Find(Worksheets("Sheet1").Range(Cel.Address).Value, LookIn:=xlWhole)
FirstValue = Worksheets("Sheet2").Range(c.Address).Offset(0, 3).Value
Set b = Worksheets("Sheet2").Columns("A:A").FindNext(c)
SecondValue = Worksheets("Sheet2").Range(b.Address).Offset(0, 3).Value
If FirstValue <> SecondValue Then
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Both"
Else
Do While FirstValue = SecondValue
Set b = Worksheets("Sheet2").Columns("A:A").FindNext(c)
SecondValue = Worksheets("Sheet2").Range(b.Address).Offset(0, 3).Value
If FirstValue <> SecondValue Then
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 3).Value = "Both"
Else
End If
Loop
End If
Next Cel
End Sub

Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
I answered your first post, like I said, its not clean, because I didn't feel like starting over. Hope you are happy with it.
 
Upvote 0
I may not have understood what you were trying to do, it would help if you said what you had as data and what you wanted to happen!

This code will put a "Both" to the right of a matched value from sheet1 and sheet2 on sheet1. JSW

Sub ProductServiceBoth()
Dim FirstValue, SecondValue As String
Dim Cel As Range

For Each Cel In Worksheets("Sheet1").Range("A:A")
FirstValue = Worksheets("Sheet1").Range(Cel.Address).Value
SecondValue = Worksheets("Sheet2").Range(Cel.Address).Value

If IsEmpty(FirstValue) Then GoTo Kill

If FirstValue = SecondValue Then
Worksheets("Sheet1").Range(Cel.Address).Offset(0, 1).Value = "Both"
End If
Next Cel
Kill:
End
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,497
Messages
6,114,002
Members
448,543
Latest member
MartinLarkin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top