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

Convert Vertical Range to Horizontal with VBA

September 12, 2015 Marcus Small
Photo by Casey Horner on Unsplash

Photo by Casey Horner on Unsplash

I was recently asked by a my friend Joe to help him with an Excel problem.  He wanted to convert data in a vertical list into a horizontal list. This sort of thing is one of those things which is usually done with a pivot table.  What if your data is a unique ID and clients which can be attached to the same unique id.

Original

IDCompany
001 Company A
001 Company A
002 Company B
003 Company D
003 Company E
003 Company F


Result

IDClient 1Client 2Client 3
001Company A Company A 
002 Company B   
001 Company D Company E Company F

To do this you could take a number of approaches.  The one I would choose would be to use the scripting dictionary to trap the unique ids and assign companies to those IDs horizontally.  This requires that the size of the array where the data is stored needs to be flexible.

Option Explicit

Sub CreateHorizontal() 'VBA to convert a verticle range into a horizontal range sorted by UID.
Dim dic As Object
Dim ar As Variant
Dim var As Variant
Dim r As Range
Dim i As Integer

   Set dic = CreateObject("Scripting.Dictionary") 'Create Storage (dict)
   With Sheet1
      For Each r In .Range("a2", .Range("a" & Rows.Count).End(xlUp))
         If Not IsEmpty(r) Then
            If Not dic.exists(r.Value) Then
               ReDim ar(1)
               ar(0) = r.Value: ar(1) = r.Offset(, 1).Value
               dic.Add r.Value, ar
            Else
               ar = dic(r.Value)
               ReDim Preserve ar(UBound(ar) + 1)
               ar(UBound(ar)) = r.Offset(, 1).Value
               dic(r.Value) = ar
            End If
         End If
      Next
   End With
   var = dic.items: Set dic = Nothing
   With Sheet3.Range("A2") 'Store the result
      .CurrentRegion.Offset(1).ClearContents
      For i = 0 To UBound(var)
         .Offset(i).Resize(, UBound(var(i)) + 1) = var(i)
      Next
   End With
End Sub

The Excel file to achieve this task with the scripting dictionary is attached. The opposite of this can also be done and the approach is outlined in the following artical Horizontal to Vertical Range in Excel.

VerticaltoHorizontal.xls

← Import Website Data to ExcelFilter Source Data in Excel with Slicer →

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.