Sort data without changing the set of each data (using VBA)

feni1388

Board Regular
Joined
Feb 19, 2018
Messages
127
Office Version
  1. 2021
Platform
  1. Windows
Hello everyone...
I have data that needs to be sorted based on date and its number of occurance.
The user copy and paste one set of data one by one using VBA. So the date is random. Can be 2024/6/12 at the end of the rows or perhaps in the middle.
So I need to sort it first by date and then the number of occurance. But I still can't find a way to sort it without changing the set of each data.
I don't know how to do it manually, let alone using VBA.
Any idea how to do this?
Please help.

Book1
ABCDEF
1DateCustomer codeNo. of itemsItem codeQtyNumber of occurance
22024/6/14CHO010181GH07101
32GL781 
43GH731 
52024/6/13CMX010271GH07101
62GL781 
73GH731 
84GL832 
92024/6/14CNB010641BM43152
Sheet1
Cell Formulas
RangeFormula
C2,C5,C9C2=IFERROR(IF(D2<>"",1,""),"")
C3:C4,C6:C8C3=IF(D3="","",IF(AND(D3<>"",C2=""),1,C2+1))
F2:F9F2=IF(A2="","",IF(A2=A2,COUNTIF($A$2:A2,A2),""))


I want it to be like below

Book1
ABCDEF
1DateCustomer codeNo. of itemsItem codeQtyNumber of occurance
22024/6/14CHO010181GH07101
32GL781 
43GH731 
52024/6/14CNB010641BM43152
62024/6/13CMX010271GH07101
72GL781 
83GH731 
94GL832 
Sheet1
Cell Formulas
RangeFormula
C2,C5:C6C2=IFERROR(IF(D2<>"",1,""),"")
C3:C4,C7:C9C3=IF(D3="","",IF(AND(D3<>"",C2=""),1,C2+1))
F2:F9F2=IF(A2="","",IF(A2=A2,COUNTIF($A$2:A2,A2),""))
 
Ahhh,
This should do.
Code:
Sub test()
    Dim a, i&, temp
    Application.ScreenUpdating = False
    a = Range("a2", Range("a" & Rows.Count).End(xlUp)).Value
    For i = 1 To UBound(a, 1)
        If a(i, 1) <> "" Then temp = CLng(a(i, 1))
        a(i, 1) = temp + i / (UBound(a, 1) * 100)
    Next
    Columns(1).Insert
    Columns(1).NumberFormat = ""
    [a2].Resize(UBound(a, 1)) = a
    [a1].CurrentRegion.Sort [a1], 1, , , , , , 1
    Columns(1).Delete
    Application.ScreenUpdating = True
End Sub

Thank you so much.... It works great ...
May I know what the problem was? Was it something that I added that cause the sort didn't work as usual?
 
Upvote 0

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
That was "MySort".

The data you provided initially absolutely coincidentally matched the sort.

So, I just used the method I used in another thread few days ago, and this should give no harm to any formula if used.

HTH.
 
Upvote 0
That was "MySort".

The data you provided initially absolutely coincidentally matched the sort.

So, I just used the method I used in another thread few days ago, and this should give no harm to any formula if used.

HTH.
I see.... thank you for your explanation.
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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