Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

move highlighted rows code.

Status
Not open for further replies.

autex

Technical User
Jan 11, 2005
75
US
I have a problem with the following code. this code looks for highlighted rows and moves them to the top. It works great unless only one row is highlighted. I get a paste error when j=2 and iInsertedrows=1 and then lose a row. The highligted row moves to the top, but a row dissapears.

j = Range("A1").End(xlDown).Row
Application.ScreenUpdating = False
For j = j To 2 Step -1
If Cells(j, 1).Interior.Color = RGB(255, 255, 0) Then
Rows(j).Cut
Rows(2).Insert Shift:=xlDown
j = j + 1
iInsertedRows = iInsertedRows + 1
If iInsertedRows = j Then Exit For
End If
Next j

The logic I need is not obvious to me. Probably because this isn't completely my own code. I have j only step down to 2 because I have a header row. Any advice?
 
changing my final step from 2 to 3 seems to do the trick. Anybody see a problem with that?
 
Anybody see a problem with that
No, as you don't care the 1st row:
either this is an already moved one or no highlighted row was below it.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
thanks validation is nice
 
For j = j To 2 Step -1

Reasigning j to j seems a rather odd way to do it. For debugging purposes using another variable for storage of the last row could be a better practice unless I'm missing some trick you're trying to accomplish.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top