Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7615391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:38:04+00:00 2026-05-31T02:38:04+00:00

I have a range of cells in Excel that is more than one column

  • 0

I have a range of cells in Excel that is more than one column wide and more than one row long. Some of the cells are blank. I would like to merge (using VBA) the non-blank cells into a list, remove the duplicates, and sort alphabetically.

For example, given this input (where a dash designates an empty cell for the purpose of this question):

-  -  A  D  -
C  -  -  A  -
-  -  B  -  D
-  -  -  -  -
A  -  -  E  -

The following sorted output is produced:

A
B
C
D
E

As the example input shows, some of the rows and columns in the range may contain all empty cells.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:38:06+00:00Added an answer on May 31, 2026 at 2:38 am

    Here is one way to do it.

    CODE

    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet
        Dim LastRow As Long, lastCol As Long, i as Long
        Dim Rng As Range, aCell As Range
        Dim MyCol As New Collection
        
        '~~> Change this to the relevant sheet name
        Set ws = Sheets("Sheet21")
        
        With ws
            LastRow = .Cells.Find(What:="*", After:=.Range("A1"), _
            Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, MatchCase:=False).Row
            
            lastCol = .Cells.Find(What:="*", After:=.Range("A1"), _
            Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, MatchCase:=False).Column
            
            Set Rng = .Range("A1:" & Split(.Cells(, lastCol).Address, "$")(1) & LastRow)
            
            'Debug.Print Rng.Address
            For Each aCell In Rng
                If Not Len(Trim(aCell.Value)) = 0 Then
                    On Error Resume Next
                    MyCol.Add aCell.Value, """" & aCell.Value & """"
                    On Error GoTo 0
                End If
            Next
            
            .Cells.ClearContents
            
            For i = 1 To MyCol.Count
                .Range("A" & i).Value = MyCol.Item(i)
            Next i
            
            '~~> OPTIONAL (In Case you want to sort the data)
            .Columns(1).Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        End With
    End Sub
    

    SNAPSHOTS

    enter image description here

    FOLLOWUP

    I just realized that adding 3 lines more makes this code even faster than the above code.

    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet
        Dim LastRow As Long, lastCol As Long, i As Long
        Dim Rng As Range, aCell As Range, delRange As Range '<~~ Added This
        Dim MyCol As New Collection
    
        '~~> Change this to the relevant sheet name
        Set ws = Sheets("Sheet1")
    
        With ws
            '~~> Get all the blank cells
            Set delRange = .Cells.SpecialCells(xlCellTypeBlanks)  '<~~ Added This
            
            '~~> Delete the blank cells
            If Not delRange Is Nothing Then delRange.Delete  '<~~ Added This
            
            LastRow = .Cells.Find(What:="*", After:=.Range("A1"), _
            Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByRows, _
            SearchDirection:=xlPrevious, MatchCase:=False).Row
    
            lastCol = .Cells.Find(What:="*", After:=.Range("A1"), _
            Lookat:=xlPart, LookIn:=xlFormulas, SearchOrder:=xlByColumns, _
            SearchDirection:=xlPrevious, MatchCase:=False).Column
            
            Set Rng = .Range("A1:" & Split(.Cells(, lastCol).Address, "$")(1) & LastRow)
    
            'Debug.Print Rng.Address
            For Each aCell In Rng
                If Not Len(Trim(aCell.Value)) = 0 Then
                    On Error Resume Next
                    MyCol.Add aCell.Value, """" & aCell.Value & """"
                    On Error GoTo 0
                End If
            Next
    
            .Cells.ClearContents
    
            For i = 1 To MyCol.Count
                .Range("A" & i).Value = MyCol.Item(i)
            Next i
    
            '~~> OPTIONAL (In Case you want to sort the data)
            .Columns(1).Sort Key1:=.Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
            OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
            DataOption1:=xlSortNormal
        End With
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Excel 2003 Question: I'd like to have one cell represent a range of cells.
I have a range, containing numeric values & blank cells. Some of the numeric
I have a range of cells in excel with names of people on the
I have 2 questions. I want to clear a range of cells in Excel
I have a form on an Excel sheet that has two mandatory cells which
i have a report that i would like to send via excel. it will
I have the following to send an email, with a range of cells from
I have the following code, to calculate the max in a range of cells:
I have locked cells in a spreadsheet using this lines of code. Range(A1:D23).Select Selection.Locked
I have range of numbers that range from 1 - 0.00000X . Most are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.