How to stop blank rows posting at top of custom sort macro

DaveLansley

New Member
Joined
Apr 2, 2019
Messages
4
Hi

When running the below code, I want to sort first by column B and then by column A, with results appearing at the top of the worksheet (underneath the header). Instead, there are lots of blank rows before the results actually appear. I know very little about VBA and was hoping someone could make the blank rows disappear!

Sub sort()
'Descending sort on A:H using columns B and A, all sheets in workbook
Dim WS As Worksheet
For Each WS In Worksheets
WS.Columns("A:H").Sort Key1:=WS.Columns("B"), Order1:=xlAscending, _
Key2:=WS.Columns(“A”), Order2:=xlAscending, Header:=x1No
Next WS
End Sub
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Sub sort()
'Descending sort on A:H using columns B and A, all sheets in workbook
Dim WS As Worksheet
For Each WS In Worksheets
WS.Columns("A:H").Sort Key1:=WS.Columns("B"), Order1:=xlAscending, _
Key2:=WS.Columns(“A”), Order2:=xlAscending, Header:=x1No
for j=1 to 1000
if cells(1,1)="" then....code to delete row
next j
Next WS
End Sub
 
Upvote 0
Sub sort()
'Descending sort on A:H using columns B and A, all sheets in workbook
Dim WS As Worksheet
For Each WS In Worksheets
WS.Columns("A:H").Sort Key1:=WS.Columns("B"), Order1:=xlAscending, _
Key2:=WS.Columns(“A”), Order2:=xlAscending, Header:=x1No
for j=1 to 1000
if cells(1,1)="" then....code to delete row
next j
Next WS
End Sub

Thank you, but what would the code to delete the row be? Would Selection.EntireRow.Delete work?
 
Upvote 0
this macro deletes row 20

Sub Macro4()
'
' Macro4 Macro
' Macro recorded 03/04/2019 by bob
'


'
Rows("20:20").Select
Selection.Delete Shift:=xlUp
End Sub
 
Upvote 0
noticed an error - should be if cells(j,1)=""..................

so the code is

rows(j).select
Selection.Delete Shift:=xlUp
 
Upvote 0
Hi

I'm afraid it's not working as I input around 50 rows of data (columns A-H all completed), ran the macro and it ended up deleting most of the rows, leaving me with only four. Here's the code:

Sub sort()
'Descending sort on A:H using columns B and A, all sheets in workbook
Dim WS As Worksheet
For Each WS In Worksheets
WS.Columns("A:H").sort Key1:=WS.Columns("B"), Order1:=xlAscending, _
Key2:=WS.Columns("A"), Order2:=xlAscending, Header:=x1Yes
For j = 1 To 100
If Cells(j, 1) = "" Then Rows(j).Select
Selection.Delete Shift:=xlUp
Next j



Next WS
End Sub
 
Upvote 0
maybe deleting rows affects the loop - so try

10 For j = 1 To 100
If Cells(j, 1) = "" Then Rows(j).Select
Selection.Delete Shift:=xlUp:goto 10
Next j
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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