Macro help?

John Davis

Well-known Member
Joined
Sep 11, 2007
Messages
3,457
Hello all:

I have the following, which does absolutely nothing, although the If/And statements are True.

Code:
Sub test()
Dim i As Long
Dim lr As Long

lr = Cells(Rows.Count, 1).End(xlUp).Row

    For i = lr To 2 Step -1
    
        If Range("C" & i).Value = "Yes" And Range("A" & i).Value = Range("D1").Value And Range("A" & i).Value = Range("A" & i).Offset(1, 0).Value Then
  
            Range("A" & i).select
                        
            MsgBox ("Value found at Row " & ActiveCell.Row & "! Stop")
            
        End If
        
    Next

End Sub

Perhaps someone can see something I missed or not understanding.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Hi

Part of your If statement says

Range("A" & i).Value = Range("D1").Value And Range("A" & i).Value = Range("A" & i).Offset(1, 0).Value

Unless Range("D1") and Range("A" & i).Offset(1,0).Value are the same, this statement will always return a false.

Also, it is a good idea to use ()

For example

If (somthing = somthing) And (somthing else = something else) Then
.
.
.
.
 
Upvote 0
Hi

Part of your If statement says

Range("A" & i).Value = Range("D1").Value And Range("A" & i).Value = Range("A" & i).Offset(1, 0).Value

Unless Range("D1") and Range("A" & i).Offset(1,0).Value are the same, this statement will always return a false.

Also, it is a good idea to use ()

For example

If (somthing = somthing) And (somthing else = something else) Then
.
.
.
.

Thanks for your reply Sandeep. It helped me to find my mistake?
 
Upvote 0
Hi there,

The syntax looks fine, though while most of it is dynamic with the row number, the value in the second AND argument is fixed to D1, perhaps it should be "D" & i

I'd also put Exit Sub immediately beneath the msgbox so the code actually does quit when a match is found.

HTH

Robert
 
Upvote 0
Hi there,

The syntax looks fine, though while most of it is dynamic with the row number, the value in the second AND argument is fixed to D1, perhaps it should be "D" & i

I'd also put Exit Sub immediately beneath the msgbox so the code actually does quit when a match is found.

HTH

Robert

Thanks for you input Robert. The value in the second And argument should be fixed, but it should have been D2 ("There's a header row"). I'm going blind, these days. Exit Sub sounds like a great idea.
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,839
Members
452,948
Latest member
UsmanAli786

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