if condition on row numbers

juliet_jia

New Member
Joined
Sep 1, 2011
Messages
7
Hi all, in the VBA macro below I'm trying to run a if...then conditional on the row numbers, but it always masses up (sometimes it works, and sometimes not..... seems random). Any help is very appreciated!

It uses findNext to identify the row numbers. To avoid findNext goes back to the beginning of the data, I thought to use If condition, i.e. If FindNext returns lastCell < FirstCell, I set the lastCell to the last row of the file.

But when lastCell is clearly larger than FirstCell, it still enters into the If loop and returns the last cell of the file as appose to should-be Findnext's lastCell.

Code:
Dim c_end As String
Dim FirstCell As String, lastCell As String
Dim Status As Variant

 Range("b1048576").End(xlUp).Select
 c_end = Selection.Row

Set Status =Range("B:B").Find("Ship", lookat:=xlWhole)                   
 j = Application.CountIf(Range("B:B"), "Ship")
            
If Not Status Is Nothing Then
                            
  For i = 1 To j

   FirstCell = Status.Row
   Set Status = Range("B:B").FindNext(Status)
   lastCell = Status.Row
 
     If [COLOR=DarkRed]lastCell < FirstCell[/COLOR] Then
      lastCell = c_end
     End If
 
  Next

End If
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I won't lie, I don't really understand what you are trying to do exactly. But I'm just curious, whi did you set "FirstCell" and "LastCell" to string values if they are going to be row numbers? I'm not sure string values evaluate perfectly in numerical operations (i.e. lastcell < firstcell) so maybe that's the cause of the problem. Try setting them to Long instead of String and see what happens.
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,266
Members
452,902
Latest member
Knuddeluff

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