VBA sorting, stubburn blanks

Portwolf

New Member
Joined
Nov 19, 2016
Messages
6
Hello!

I've got a little issue here.
I am pasting data from a sheet into another sheet to order it there:

Code:
Sub Workbook_Open()

    Application.ScreenUpdating = False

        Worksheets("OrdLoj").Range("A9:C140").Copy
        Worksheets("preplj").Range("A1").PasteSpecial Paste:=xlPasteValues

    Application.ScreenUpdating = True

End Sub

Well, that works, and it pastes 3 collumns of data with about 30 rows.
It might grow on the source, so i cannot lessen the copy range.
I wanted to sort the data on the paste side, but every time i do it, the ordering sends all the data on the bottom of a whole bunch of blank rows.
As i am looking at this, i see that it is ordering the ammount of cells previously pasted, even if no reference to them on the code..


Needed:
- order the data based on the "C" column value, from highest to lowest.
- set the data on the top ignoring blanks.

What it looks like after paste:

store 1012
store 2110
store 317
store 416
store 5010
store 6011
store 7011

<tbody>
</tbody>


What it looks like after ordering:

store 316
store 417
store 5010
store 2110
store 6011
store 7011
store 1012

<tbody>
</tbody>

What i need (ordered by the last column, other values are not important)

store 1012
store 6011
store 7011
store 5010
store 2110
store 417
store 316

<tbody>
</tbody>

Any ideas will be much appreciated!
Best regards!
 
Last edited by a moderator:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Does this work for you...

Code:
Private Sub Workbook_Open()
     
    Dim lRow As Long
    
    Application.ScreenUpdating = False
    Worksheets("OrdLoj").Range("A9:C140").Copy
    Worksheets("preplj").Range("A1").PasteSpecial Paste:=xlPasteValues
    lRow = Sheets("preplj").Cells(Rows.Count, 3).End(xlUp).Row
    ActiveWorkbook.Worksheets("preplj").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("preplj").Sort.SortFields.Add Key:=Range("C1:C" & lRow) _
        , SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("preplj").Sort
        .SetRange Range("A1:C" & lRow)
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
        
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
Hello igold,

Partially, it does the ordering perfectly, but it send the info to the bottom, having a LOT of empty rows until it gets there....
Just like in table 2 of my example, but on perfect order like in table 3.

So, it's half working :D

Thanks!
 
Upvote 0
Unless I am missing something, if you sort descending on column C the blank rows should fall to the bottom of the sheet...
 
Last edited:
Upvote 0
Got it.

Added:

Code:
    On Error Resume Next
    With Range("A1:C140")
    .Value = .Value
    .SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End With

And now it's fixed :D

Thank you for the help!
 
Upvote 0
Happy to help. I am glad you got it squared away. Thanks for the feedback!
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

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