Advanced Excel Techniques: Data Transformation and Lookup
Merging Duplicate Cells in a Column
Before merging identical cells, sort the column to group duplicates.
- Press
Alt+F11to open the VBA editor. - Insert a new module and paste the code below.
- Return to the worksheet, go to View > Macros > View Macros, select the macro, and run it. The workbook may need to be saved as
.xlsm. - When prompted, enter the column number (not letter) to merge.
Sub MergeSameCells()
Dim lastRow As Long, i As Long
Application.DisplayAlerts = False
Dim col As Integer
col = InputBox("Enter the column number to merge")
lastRow = Cells(Rows.Count, col).End(xlUp).Row
For i = lastRow To 2 Step -1
If Cells(i, col).Value = Cells(i - 1, col).Value Then
Range(Cells(i - 1, col), Cells(i, col)).Merge
End If
Next i
Application.DisplayAlerts = True
End Sub
Extracting Numbers from Mixed Text
Use a custom VBA funcsion with regex:
Function ExtractNumbers(cellRef As Range) As String
With CreateObject("vbscript.regexp")
.Pattern = "[^\d.-]+"
.Global = True
ExtractNumbers = .Replace(cellRef.Value, " ")
End With
End Function
Usage: =ExtractNumbers(A1) returns only numeric characters.
Dynamic Sequential Numbering After Filtering
To maintain a sequential row count that remains correct after filtering, enter this formula in cell A2 and fill down:
=SUBTOTAL(103, B$2:B2)
The 103 parameter counts visible cells, ignoring hidden rows.
Transposing Data into Fixed-Column Blocks
Use INDEX with calculated row and column offsets.
Example: Convert one row into multiple rows of 10 columns:
=INDEX($1:$1, 1, COLUMN(A1) + (ROW(A1)-1)*10)
Fill right and down.
Example: Print labels from a vertical list (3 columns per card, 4 records per page):
=IF(INDEX(Data!$A:$C, QUOTIENT(ROW(A1)-1, 3)*4 + COLUMN(A1) + 1, IF(MOD(ROW(A1), 3)=0, 3, MOD(ROW(A1), 3)))=0, "", INDEX(Data!$A:$C, QUOTIENT(ROW(A1)-1, 3)*4 + COLUMN(A1) + 1, IF(MOD(ROW(A1), 3)=0, 3, MOD(ROW(A1), 3))))
To insert separator rows every fourth row:
=IF(MOD(ROW(A1), 4)=0, " - - - - - - ", IF(INDEX(Data!$A:$C, QUOTIENT(ROW(A1)-1, 4)*4 + COLUMN(A1) + 1, MOD(ROW(A1), 4))=0, "", " " & INDEX(Data!$A:$C, QUOTIENT(ROW(A1)-1, 4)*4 + COLUMN(A1) + 1, MOD(ROW(A1), 4))))
Flexible Lookup with INDEX + MATCH
Unlike VLOOKUP, this combination works regardless of column order and preserves leading zeros.
=INDEX(ReturnColumn, MATCH(LookupValue, LookupColumn, 0))
Example: Match a student ID from column C to column H and return the corresponding name from column G:
=INDEX(G:G, MATCH(C3, H:H, 0))
Extracting All Matches for Repeated Criteria
When you need to retrieve all occurrences (not just the first), use an array formula.
Task: In row 2, column A–N contains letters; find all positions of "a" and return the value from row 1 above them.
Formula in A4 (array-entered with Ctrl+Shift+Enter):
=INDEX(1:1, SMALL(IF($A2:$N2="a", COLUMN($A:$N), 256), COLUMN(A1))) & ""
Fill right until blanks appear.
Example: List all students of a given teacher:
Assume StudentData sheet: column B = student names, colum U = teacher names (<5000 rows).
Enter in C2 (array-entered), then fill right and down:
=INDEX(StudentData!$B:$B, SMALL(IF(StudentData!$U:$U=$A2, ROW(StudentData!$B:$B), 5000), COLUMN(A1))) & ""
To concatenate the results into one cell, use TEXTJOIN:
=TEXTJOIN(", ", TRUE, C2:AZ2)
Example: Auto-updating filtered data to another sheet:
In the target sheet, place headers identical to the source sheet (named RawData). In A2, array-enter:
=INDEX(RawData!A:A, SMALL(IF(RawData!$DZ$1:$DZ$2151="Yes", ROW($1:$2151), 6666), ROW(A1))) & ""
Fill down enough rows. Then in B2, use VLOOKUP to pull other columns:
=IFNA(VLOOKUP($A2, RawData!$A:$DQ, COLUMN(), 0), "")
Fill right and down.
Multiple conditions: Replace the IF condition with OR(condition1, condition2) or AND(condition1, condition2).
Adaptive sheet name: Use CELL("filename", A1) to extract the sheet name and feed it into INDIRECT:
=INDEX(RawData!A:A, SMALL(IF(INDIRECT("'RawData'!" & RIGHT(CELL("filename", A1), 1) & "$1:" & RIGHT(CELL("filename"), 1) & "$2151")="Yes", ROW($1:$2151), 6666), ROW(A1))) & ""
Custom ConTxt Function: Concatenate Range Contents
Public Function ConTxt(ParamArray args() As Variant) As Variant
Dim tmptext As String, i As Variant, cell As Range
tmptext = ""
For i = 0 To UBound(args)
If Not IsMissing(args(i)) Then
Select Case TypeName(args(i))
Case "Range"
For Each cell In args(i)
tmptext = tmptext & cell.Value
Next cell
Case "Variant()"
Dim v As Variant
For Each v In args(i)
tmptext = tmptext & v
Next v
Case Else
tmptext = tmptext & args(i)
End Select
End If
Next i
ConTxt = tmptext
End Function
Use in worksheet: =ConTxt(A1:A10)
INDIRECT: Reference Cells via Text Strings
INDIRECT(ref_text, [a1]) evaluates a text string as a cell reference.
- A1 style:
=INDIRECT("Sheet2!B1")returns the value of B1 on Sheet2. - Dynamic sheet names:
=INDIRECT("Sheet" & A1 & "!B1")where A1 holds the sheet number.
Getting Workbook and Sheet Names
Use CELL("filename", A1) to return the full path including workbook and sheet name.
Extract sheet name only:
=MID(CELL("filename"), FIND("]", CELL("filename")) + 1, 256)
Adjust the last argument to exact length if needed.