Author: Oscar Cronquist Article last updated on September 18, 2019

This article demonstrates a macro that returns cell references for cell ranges populated with values on a worksheet.

Jinesh asks:

I have to combine 200 columns into one list. I know. I tried steps from 'Combine cell ranges into a single range while eliminating blanks' UDF, but looks like typing the formula itself is going to be a big deal. Any advice?

(To give a bit of a background, I am trying to compare 200 columns to one column of data and figured it would be easier if I combine all 200 into one column and then compare, it would be easy).

What you will learn in this article

  1. Create a macro that extracts cell references of populated cells in a worksheet.
  2. Save the answer from an inputbox to a variable.
  3. Loop through populated cells in a column.
  4. How to use the currentregion property with an object reference.
  5. Iterate through worksheet columns.
  6. Append values to a variable.
  7. Add values to a collection variable.
  8. Insert a new worksheet and save an object reference to a variable.
  9. Save the result to a given cell.

The following macro moves from column to column and checks for values. If a value is found, the current region property (Ctrl + A) is applied and the cell range address is saved. A new sheet is created and all unique distinct cell references are concatenated using a delimiting character into cell A1.

The current region is a range bounded by any combination of blank rows and blank columns. In other words, the macro creates cell references to all cell ranges populated with values.

VBA Code

'Name macro
Sub ExtractAddresses()

'Dimension variables and declare data types
Dim sht As Worksheet
Dim CurCell As Range
Dim Adr As New Collection
Dim c As Single
Dim Value As Variant
Dim result As String, delch As String

'Show inputbox and ask for a delimiting character, save to variable delch
delch = InputBox("Delimiting character:")

'The SET statement allows you to save an object reference to a variable
Set CurCell = ActiveSheet.Range("A1")

'Iterate from 1 to the number of columns in your workbook using the FOR NEXT statement
For c = 1 To Columns.Count - 1

    'The SET statement allows you to save an object reference to a variable, in this case it is next populated cell in column A. It returns the last cell if column A has no populated cells.
    Set CurCell = CurCell.End(xlDown)

    'Loop through following lines as long as cell saved to CurCell is not empty. 
    Do While CurCell.Value <> ""

        'Check if the length of the address of object CurCell with currentregion property is larger than 0 (zero)
        If Len(CurCell.CurrentRegion.Address) > 0 Then

            'Enable error handling, an error occurs if a cell reference already exists in the collection variable
            On Error Resume Next
 
            'Save address of currentregion property based on object CurCell to collection variable Adr
            Adr.Add CurCell.CurrentRegion.Address, CStr(CurCell.CurrentRegion.Address)

            'Disable error handling
            On Error GoTo 0
        End If

        'Check if the CurCell row number equals the last row number in workbbok, if so stop Loop
        If CurCell.Row = Rows.Count Then Exit Do

        'The SET statement allows you to save an object reference to a variable, in this case it is the last cell in column A.
        Set CurCell = CurCell.End(xlDown)
    Loop

        'Save an object reference to the next cell to the right based on variable c to variable CurCell
        Set CurCell = Range("A1").Offset(0, c)

'Continue with next number 
Next c

'Iterate through each value saved in collection variable Adr
For Each Value In Adr
    'Add value to string variable result using variable delch as a delimiting character
    result = result & delch & Value

'Continue with next value in collection
Next Value

'Add a new sheet and save a reference to variable sht
Set sht = Sheets.Add

'Save text in string result to cell A1
sht.Range("A1") = Right(result, Len(result) - 1)
End Sub

Where to put the code?

  1. Copy above VBA code.
  2. Press Alt + F11 to open the Visual Basic Editor.
  3. Select your workbook in the Project Explorer.
  4. Click "Insert" on the menu.
  5. Click "Module" to create a code module named Module1 that will be displayed below "Modules" in the Project Explorer.
  6. Paste VBA code to the code window, see image above.
  7. Return to Excel.

Note, save your workbook with file extension *.xlsm (macro-enabled) to attach the VBA code to the workbook.

How to use the macro

The animated image above shows how to run the macro.

  1. Press Alt + F8 to open the macro dialog box.
  2. Select ExtractAddresses.
  3. Click on button "Run".
  4. The macro asks for a delimiting character.
  5. The macro creates a new worksheet and populates cell A1 with cell references containing values. These cell references have a comma as a delimiting character.

Download Excel file


* You will also get a weekly newsletter, unsubscribe anytime!