View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Farooq Sheri Farooq Sheri is offline
external usenet poster
 
Posts: 37
Default Help with VB code to compare two columns

I have written VB code to compare values in columns B and S. If a value in B
is not found in S then the text color for value in B is changed to red. The
process continues until there are no more values in B. The code is given
below:

Range("B2").Activate
Dim ctr As Integer
ctr = 0
Set rng = Range("B2")
Dim x As String
x = ActiveCell.Text
Debug.Print x

While x < ""

Columns("S:S").Select
On Error GoTo errorhandler
Selection.Find(What:=x, After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate


errorhandler:
If x < ActiveCell.Value Then
MsgBox ("Current Value is not found")
rng = Worksheets("Sheet3").Cells(2, 2).Offset(ctr, 0).Activate
Selection.Font.ColorIndex = 3
End If

ctr = ctr + 1
rng = Worksheets("Sheet3").Cells(2, 2).Offset(ctr, 0).Activate
x = ActiveCell.Text
Wend

This is what is happening. For the first value in B not found in S the code
works okay, but for the next value not found in S, the following error is
generated "Object Variable or With block not set". How can I overcome this
problem (I even have an error handler in place). Importantly what is causing
this error.

Thanks in advance for your help.

Farooq