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
Create a macro that extracts cell references of populated cells in a worksheet.
Save the answer from an inputbox to a variable.
Loop through populated cells in a column.
How to use the currentregion property with an object reference.
Iterate through worksheet columns.
Append values to a variable.
Add values to a collection variable.
Insert a new worksheet and save an object reference to a variable.
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.
'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
'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)
'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
'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
'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)
Where to put the code?
Copy above VBA code.
Press Alt + F11 to open the Visual Basic Editor.
Select your workbook in the Project Explorer.
Click "Insert" on the menu.
Click "Module" to create a code module named Module1 that will be displayed below "Modules" in the Project Explorer.
Paste VBA code to the code window, see image above.
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.
Press Alt + F8 to open the macro dialog box.
Click on button "Run".
The macro asks for a delimiting character.
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.