Sort virtual range

Vincent88

Active Member
Joined
Mar 5, 2021
Messages
382
Office Version
  1. 2019
Platform
  1. Windows
  2. Mobile
Hi Guy,
I want to sort a virtual list. Can someone help me out.
The virtual object list refers to dynamic range in Column L. I want to sort the object list only (not actual list in column L) from A to Z and assign a name to it for dropdown list purpose. What should be corrected ?

VBA Code:
Sub sortedVirtualListAgent1()

Dim rngSort As Range
Dim lr As Long
Dim Agtarray As Object
Dim rngL As Range

'Creating a array list
Set Agtarray = CreateObject("System.Collections.ArrayList")

'Physical Source in Column L
lr = Sheets("Data").Cells(Rows.Count, "L").End(xlUp).Row
Set rngL = Sheets("Data").Range("L2:L" & lr)

  'Set reference to the Table and column to be sorted
      Set Agtarray = rngL
    

  'Use Range.Sort method to apply sort
    
    Agtarray.Sort _
    Key1:=rngSort, _
    Order1:=xlAscending, _
    Header:=xlYes
        
    sorted_array = Agtarray.toarray
    
    End Sub
 
Agtarray.toarray creates a zero-based array so you need

Rich (BB code):
Sheets("Data").Range("N1").Resize(UBound(Sorted_array) + 1, 1).Value = Application.Transpose(Sorted_array)
 
Upvote 0
Solution

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Agtarray.toarray creates a zero-based array so you need

Rich (BB code):
Sheets("Data").Range("N1").Resize(UBound(Sorted_array) + 1, 1).Value = Application.Transpose(Sorted_array)
Thanks Peter_SSs.
I create another thread to ask for help as part 2 of this code, please review if you have time. Thanks in advance.
 
Upvote 0

Forum statistics

Threads
1,215,051
Messages
6,122,872
Members
449,097
Latest member
dbomb1414

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