why code gives error when just change range

Hasson

Active Member
Joined
Apr 8, 2021
Messages
390
Office Version
  1. 2016
Platform
  1. Windows
Hi
I have this code sort the data in sheet file based on sheet RP as in orginal code works , but when change the range from A: C to A: D will gives error in this line
VBA Code:
Application.AddCustomList ListArray:=CustSort
as in picture
VBA Code:
Sub test()
    Dim SortRng As Range, SortKey As Range
    Dim SortRng2 As Range, SortKey2 As Range
    Dim CustSort As Variant

    Set SortRng = Sheets("file1").Range("A1:D" & Sheets("file1").Range("A" & Rows.Count).End(xlUp).Row)
    Set SortKey = Sheets("file1").Range("B1")
  
    CustSort = Sheets("RP").Range("B2:B" & Sheets("RP").Range("B" & Rows.Count).End(xlUp).Row).Value
    Application.AddCustomList ListArray:=CustSort
    
    SortRng.Sort Key1:=SortKey, Order1:=xlAscending, Header:=xlGuess, _
        OrderCustom:=Application.CustomListCount + 1, MatchCase:=False, _
        Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
    
    Application.DeleteCustomList Application.CustomListCount
End Sub
what's the mistake guys
er.PNG
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Hi,
I can see that error when I'm about to add a Range object to the custom list, but in the code, a value property has been used to make it an array, so it should work...odd.
As another plan, instead of using a custom list, give using SortField objects a try if the situation allows.

VBA Code:
Sub test2()
    Dim SortRng As Range, SortKey As Range
    Dim SortRng2 As Range, SortKey2 As Range
    Dim CustSort As Variant

    Set SortRng = Sheets("file1").Range("A1:D" & Sheets("file1").Range("A" & Rows.Count).End(xlUp).Row)
    Set SortKey = Sheets("file1").Range("B1")

    CustSort = Application.Transpose(Sheets("RP").Range("B2:B" & Sheets("RP").Range("B" & Rows.Count).End(xlUp).Row).Value)
    CustSort = Join(CustSort, ",")

    With SortRng.Parent.Sort
        .SortFields.Clear
        .SortFields.Add Key:=SortRng.Columns(1), CustomOrder:="" & CustSort & "", Order:=xlAscending
        .SetRange SortRng
        .Header = xlYes
        .Apply
    End With
End Sub
 
Upvote 0
thanks, but it doesn't seem to work , also doesn't show any error.:rolleyes:
 
Upvote 0
For testing, is it possible to post data and the expected results with XL2BB? It would be a great help.
 
Upvote 0
here is the data .
RESULT (1) (1).xlsm
AB
1ITEMID
21QQW-1 MM CLA1 23M-1 IT
32QQW-2 TH NM-1 CLA2 VBG L CHI
43QQW-3 CV CLA3 TAI
54QQW-4 M*12.5 CLA4 TR
65QQW-5 CLA5 EG
76QQW-6 M230 TU
87QQW-7 S** CLA7 US
98QQW-8 CLA8 UK
109QQW-9 CLA9 N BR
1110QQW-10 BN CLA10 IT
1211QQW-10 BN CLA10 IT -MM
1312QQW-11 LVD CH
1413QQW-12 CLA12 JA
1514QQW-13 CLA13 TR
1615QQW-13 CLA11 TR
1716QQW-14 L/R CLA14 SS230 EG
1817QQW-15 CLA5 EG
RP



RESULT (1) (1).xlsm
ABCD
1ITEMIDIMPEX
21QQW-10 BN CLA10 IT -MM1212
32QQW-11 LVD CH231
43QQW-12 CLA12 JA122
54QQW-13 CLA13 TR455
65QQW-7 S** CLA7 US66
76QQW-8 CLA8 UK77
87QQW-9 CLA9 N BR88
file1



result
RESULT (1) (1).xlsm
ABCD
1ITEMIDQTYEX
21QQW-7 S** CLA7 US66
32QQW-8 CLA8 UK77
43QQW-9 CLA9 N BR88
54QQW-10 BN CLA10 IT -MM1212
65QQW-11 LVD CH231
76QQW-12 CLA12 JA122
87QQW-13 CLA13 TR455
file1


remember this is dummy data but my real data are at least 1000 rows .
thanks for your time .
 
Upvote 0
Changed the "key" columns for sorting to fit the actual worksheet.
It worked with the dummy data. Give it a try with your real data.

VBA Code:
Sub test3()
    Dim SortRng As Range, SortKey As Range
'    Dim SortRng2 As Range, SortKey2 As Range
    Dim CustSort As Variant

    Set SortRng = Sheets("file1").Range("A1:D" & Sheets("file1").Range("A" & Rows.Count).End(xlUp).Row)
    Set SortKey = Sheets("file1").Range("B1")

    CustSort = Application.Transpose(Sheets("RP").Range("B2:B" & Sheets("RP").Range("B" & Rows.Count).End(xlUp).Row).Value)
    CustSort = Join(CustSort, ",")

    With SortRng.Parent.Sort
        .SortFields.Clear
        .SortFields.Add Key:=SortRng.Columns(2), CustomOrder:="" & CustSort & "", Order:=xlAscending
        .SetRange SortRng
        .Header = xlYes
        .Apply
    End With
End Sub
 
Upvote 0
Solution
thanks but after arranging data should also re-autonumber again in column A 1,2,3,...
how could be,please?
 
Upvote 0
Just add the following code after sorting.

VBA Code:
    ' re-autonumber
    With Sheets("file1").Range("A2")
        .Value = 1
        .AutoFill Destination:=.Resize(.End(xlDown).Row - 1), Type:=xlFillSeries
    End With
 
Upvote 0
alternative code:
VBA Code:
Option Explicit
Sub test()
Dim lr&
Worksheets("file1").Activate
lr = Cells(Rows.Count, "B").End(xlUp).Row
With Range("A2:A" & lr)
    .Formula = "=MATCH(B2,RP!B:B,0)"
    .Value = .Value
End With
Range("A2:D" & lr).Sort Range("A1")
Range("A2:A" & lr).Formula = "=COUNTA($B$2:B2)"
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,848
Messages
6,121,914
Members
449,054
Latest member
luca142

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