Excel

TheSmallman.com

an XL ideas Lab

Dashboards VBA
  • Home
  • Dashboards
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
    • Power Pivot a User Guide
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About
Menu

Excel Dashboards VBA

Street Address
City, State, Zip
Phone Number
an XL ideas Lab

Your Custom Text Here

Excel Dashboards VBA

  • Home
  • Dashboards
  • Excel Tips
    • Tips & Tricks
    • Charts
    • Modelling
    • Infographics
    • VBA
  • Shop Dashboards
  • PowerPivot
    • Power Pivot a User Guide
  • Courses
    • Excel Dashboard Course
    • Advanced Dashboard Course
    • Financial Modelling Course
    • Excel VBA Course
  • Blog
  • About

Excel VBA to Sort an Array

February 23, 2015 Marcus Small

I was recently researching the sorting of an array.  I sat in my reading chair and read through John Walkenback’s excellent Power Programming tome on the subject and did some research online when I came across the brilliant .Net feature System Collections ArrayList.  What a find it was as this feature allows you to sort an array and send that array right back into Excel. 

On running some test coding I ran into a problem.   Excel would not run this line

With CreateObject("System.Collections.ArrayList")

Which was kind of frustrating as this was the active line which kicks the whole process off.  A bit more reading and I found a patch which adds the feature to your computer.  I downloaded this patch and the code ran fine.  The following is the link.

http://www.microsoft.com/en-us/download/confirmation.aspx?id=1639

This is something to remember if the coding does not run smoothly.  The following is the code which sorts an array.  A point of caution.  This coding will sort a list of only unique items.  The results of the sorted array are stored in column B.

Sub SortInArray() 'System Collections Arraylist, Sort an Excel VBA Array.
Dim ar As Variant
Dim var As Variant
Dim i As Long

   ar = [a11:a20].Value
   With CreateObject("System.Collections.ArrayList")
      For Each var In ar
         If Not .Contains(var) Then .Add var
      Next
      .Sort
      For i = 0 To .Count - 1
         Range("B" & i + 11) = .Item(i)
      Next
   End With
End Sub

You could sort the list in Excel prior to running the code, however I wanted to achieve the task inside VBA with as little fuss as possible.  

The following is an Excel file with the Collections Array List sort procedure in VBA.

SortArrayUnique.xls

Tags sort, vba, array, system collections, arraylist
← Filter Data in VBA without an Excel FilterExcel Merge Sheets to Master →

Featured Posts

Excel Dashboards: Tracking a Crisis

Excel Dashboards: Tracking a Crisis
April 14, 2020

Recent Posts

Populating an Excel Table from a Range of Cells with VBA

Populating an Excel Table from a Range of Cells with VBA June 12, 2025

Fuzzy Distribution with Randbetween

Fuzzy Distribution with Randbetween May 21, 2025

Add Minimum and Maximum for Chart in Cells

Add Minimum and Maximum for Chart in Cells March 12, 2025

Inflation Over Multiple Years in a Single Cell

Inflation Over Multiple Years in a Single Cell January 10, 2025

Hubspot Dashboard

Hubspot Dashboard October 3, 2024

Monthly Dashboard With Supporting Metrics

Monthly Dashboard With Supporting Metrics September 25, 2024

Excel Show Missing Sheet Tabs

Excel Show Missing Sheet Tabs July 29, 2024

Run Macro Overnight Automatically

Run Macro Overnight Automatically June 24, 2024

Split File into Parts and Save to Directory

Split File into Parts and Save to Directory April 20, 2024

Most Popular Author

Most Popular Author December 14, 2023

 

Follow US:

 
 

MarcusSmall@thesmallman.com

 

TheSmallman.com - Making your small systems hum...
© Copyright 2013-2024 theSmallman.com All Rights Reserved.