VBA macro to sort columns data

Yamasaki450

Board Regular
Joined
Oct 22, 2021
Messages
58
Office Version
  1. 2021
Platform
  1. Windows
Hey guys.

Sorry to bother you again but i need another macro to sort columns data like shown on screenshot. I have to mention i have large amount of data to sort its about 15 million cells so i need macro to be fast as possible...

Thanks.
 

Attachments

  • 1.png
    1.png
    46.9 KB · Views: 11

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Try this on a copy of your data:
VBA Code:
Sub Yamasaki450_1()
Dim i As Long, j As Long, k As Long
Dim va, vb
Dim c As Range
Dim t As Double

t = Timer
Set c = Range("B2:D15")  'change the range to suit
va = c
ReDim vb(1 To UBound(va, 1), 1 To UBound(va, 2))

For j = 1 To UBound(va, 2)
    k = UBound(va, 1)
    For i = UBound(va, 1) To 1 Step -1
        If va(i, j) <> Empty Then
            vb(k, j) = va(i, j)
            k = k - 1
        End If
    Next
Next

c = vb
Debug.Print "Completion time:  " & Format(Timer - t, "0.00") & " seconds"
End Sub
 
Upvote 0
Try this on a copy of your data:
VBA Code:
Sub Yamasaki450_1()
Dim i As Long, j As Long, k As Long
Dim va, vb
Dim c As Range
Dim t As Double

t = Timer
Set c = Range("B2:D15")  'change the range to suit
va = c
ReDim vb(1 To UBound(va, 1), 1 To UBound(va, 2))

For j = 1 To UBound(va, 2)
    k = UBound(va, 1)
    For i = UBound(va, 1) To 1 Step -1
        If va(i, j) <> Empty Then
            vb(k, j) = va(i, j)
            k = k - 1
        End If
    Next
Next

c = vb
Debug.Print "Completion time:  " & Format(Timer - t, "0.00") & " seconds"
End Sub
I tried this code and its really fast it only takes a minute to sort 15 million cells... But all cells with value 0 are missing... Is this possible to fix?
 

Attachments

  • 2.png
    2.png
    38.6 KB · Views: 3
Upvote 0
Did you try the code in post #19 on VBA macro to delete highlighted cells
Yes i did try your code but there i asked you to extend existing code. Your code works just fine but i still need separate code for sorting and separate for highlighting cells. I will open new thread for this...
I also tried only the sorting part of your code (in post #19) separately but it doesnt work correct in this case shown on screenshot. Thats why im asking for new one.
 
Upvote 0
But all cells with value 0 are missing... Is this possible to fix?
Ah, sorry about that.
We need to change If va(i, j) <> Empty Then to If va(i, j) <> "" Then
So, use this one:
VBA Code:
Sub Yamasaki450_2()
Dim i As Long, j As Long, k As Long
Dim va, vb
Dim c As Range
Dim t As Double

t = Timer
Set c = Range("B2:D15")  'change the range to suit
va = c
ReDim vb(1 To UBound(va, 1), 1 To UBound(va, 2))

For j = 1 To UBound(va, 2)
    k = UBound(va, 1)
    For i = UBound(va, 1) To 1 Step -1
        If va(i, j) <> "" Then
            vb(k, j) = va(i, j)
            k = k - 1
        End If
    Next
Next

c = vb
Debug.Print "Completion time:  " & Format(Timer - t, "0.00") & " seconds"
End Sub
 
Upvote 1
Solution
You're welcome, glad to help & thanks for the feedback.:)
 
Upvote 0

Forum statistics

Threads
1,215,215
Messages
6,123,668
Members
449,114
Latest member
aides

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