Remove blank rows from range

sharky12345

Well-known Member
Joined
Aug 5, 2010
Messages
3,422
Office Version
  1. 2016
Platform
  1. Windows
I'm using this to sort data into different columns depending on a cell value;

Code:
Sheet9.Range("P2:T40").ClearContents
For Each Cell In Sheet9.Range("E2:E28")
If Cell <> "" Then
If Cell.Offset(0, 3) = "N" Then
Cell.Offset(0, 11).Value = Cell
Cell.Offset(0, 12).Value = Cell.Offset(0, 1).Value
End If
If Cell.Offset(0, 3) = "Y" Then
Cell.Offset(0, 14).Value = Cell
Cell.Offset(0, 15).Value = Cell.Offset(0, 1).Value
End If
End If
Next

The issue I have is that it it leaving blank rows in the sorted data because the cells housing "N" or "Y" are not contiguous.

I know why it's doing it, just not how to resolve it if anyone can help?
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try:
Code:
Sub Test()
 Dim k  As Long
  For k = 28 To 2 Step -2
   If Cells(k, 5) <> "" Then
    If Cells(k, 8) = "N" Then
     Cells(k, 16) = Cells(k, 5)
     Cells(k, 17) = Cells(k, 6)
    ElseIf Cells(k, 8) = "Y" Then
     Cells(k, 19) = Cells(k, 5)
     Cells(k, 20) = Cells(k, 5)
    Else: Rows(k).Delete
    End If
   End If
  Next
End Sub
 
Upvote 0
Hello, and thanks - this doesn't work because it's still leaving blank rows between each of the sorted values.
 
Upvote 0
Can we have some before and after screenshots of your data please? also do your cells contain constants or formulas?
 
Upvote 0
Try this then.
Code:
Sub Test()
 Dim k  As Long
  For k = 28 To 2 Step -2
   If Cells(k, 5) <> "" Then
    If Cells(k, 8) = "N" Then
     Cells(k, 16) = Cells(k, 5)
     Cells(k, 17) = Cells(k, 6)
    ElseIf Cells(k, 8) = "Y" Then
     Cells(k, 19) = Cells(k, 5)
     Cells(k, 20) = Cells(k, 5)
    Else: Rows(k).Delete
    End If
   [COLOR=#ff0000]Else: Rows(k).Delete[/COLOR]
   End If
  Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,021
Latest member
Mohamed Magdi Tawfiq Emam

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