Run code depending on row number

wpryan

Well-known Member
Joined
May 26, 2009
Messages
534
Office Version
  1. 365
Platform
  1. Windows
Hi All, I have some code I want to run which compares the last found value in one column with another. Depending on the row number, I want to do different things. Code:

Code:
lRowWS1 = sht1.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
lRow1 = sht1.Cells(sht1.Rows.Count, "A").End(xlUp).Row
lRow2 = sht1.Cells(sht1.Rows.Count, "B").End(xlUp).Row
lRow3 = sht1.Cells(sht1.Rows.Count, "D").End(xlUp).Row
    
    If (lRow1 Or lRow2) < lRow3 Or (lRow1 Or lRow2) = lRow3 Then
        Set nRange = Union(Range("A" & lRowWS1 + 1), Range("A" & lRowWS1 - 1), _
        Range("B" & lRowWS1 + 1), Range("B" & lRowWS1 - 1), _
        Range("C" & lRowWS1 + 1), Range("C" & lRowWS1 - 1), _
        Range("D" & lRowWS1 + 1), Range("D" & lRowWS1), Range("D" & lRowWS1 - 1), _
        Range("E" & lRowWS1 + 1), Range("E" & lRowWS1), Range("E" & lRowWS1 - 1))
    Else:
        Set nRange = Union(Range("A" & lRowWS1), Range("A" & lRowWS1 - 2), _
        Range("B" & lRowWS1), Range("B" & lRowWS1 - 2), _
        Range("C" & lRowWS1), Range("C" & lRowWS1 - 2), _
        Range("D" & lRowWS1), Range("D" & lRowWS1 - 1), Range("D" & lRowWS1 - 2), _
        Range("E" & lRowWS1 ), Range("E" & lRowWS1 - 1), Range("E" & lRowWS1 - 2))
    End If

With nRange
    .Locked = False
    .FormulaHidden = False
    .ClearContents
End With
The problem is, if lRow3 is greater than lRow1 or lRow2 then the first condition is fired.
...any idea what I am doing wrong? Thanks in advance.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I'm not totally sure I follow your question here but here's a couple things I noticed.

First, it seems like your first condition
Code:
[COLOR=#333333](lRow1 Or lRow2) < lRow3 [/COLOR]
is trying to say exactly what you are saying the problem is... "If lRow3 is greater than either lRow1 or lRow2)...

Next, I dont think this
Code:
[COLOR=#333333]    If (lRow1 Or lRow2) < lRow3 Or (lRow1 Or lRow2) = lRow3 Then[/COLOR]
means what you think it does. It seem like you are trying to say "If either lRow1 or lRow2 are less than or equal to lRow3 then do something..."

I would think you would write that something like this:
Code:
If lRow1 <= lRow3 Or lRow2 <= lRow3 Then
 
Upvote 0
Thanks for your quick response, and sorry for my late response to your quick response. Your suggestion is working fine. Thanks!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,907
Messages
6,175,301
Members
452,633
Latest member
DougMo

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