VBA to deal with row based on cell value then move to next row

tommyleinen

Board Regular
Joined
Aug 22, 2009
Messages
74
I have a sheet of data that needs each row to be dealt with a certain way. To assist with this I've added a helper column (U) which is a vlookup to a set of conditions. the cell value from U10 downwards returns a value 1-6. The formula gets pasted into column U, so the next blank cell (usually around 50 - 100 rows down), is where the data ends and this can vary.

1 means delete row
2 means IF L>0 Then clear contents of M and N
3 means IF L>N then make N the value of L, and clear contents of L and M
4 means clear contents of L and M
5 means delete row unless sheetname is Sheet4
6 means clear contents of L and N

I think I can put together the code for 1, apart from making the new active cell to be U of the row after the one deleted.
2-6 look within my capability

Really I need help with 1 and looping through the rows until the next cell is blank.

Any help would be appreciated
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
When deleting or inserting rows, it is always better to work from the bottom of the sheet up. That way you aren't affecting the range you have not checked yet (and missing or duplicating any rows).

So what I typically like to do is find the last row in column U with data, and work up from there in a loop, structured like this:
VBA Code:
Dim lr as Long
Dim r as Long

'Find last row with data in column U
lr = Cells(Rows.Count, "U").End(xlUp).Row

'Loop through all rows backwards
For r = lr to 1 Step -1
'   Check the value in column U and make decision on what to do
    Select Case Cells(r, "U").Value
        Case 1
            'Do your steps here for value of 1 here
        Case 2
            'Do your steps here for value of 2 here
       ...
        Case 6
            'Do your steps here for value of 1 here
    End Select   
Next r

If you aren't familiar with CASE statements, see here: MS Excel: How to use the CASE Statement (VBA)
 
Upvote 0
Solution
Thankyou for your insight into this, I believe you have saved me a lot of frustration using your 'bottom up' strategy.

All working as intended, thankyou very much!!!
 
Upvote 0
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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