VBA to Transpose Data

jrock8859

New Member
Joined
Oct 11, 2022
Messages
5
Office Version
  1. 365
Platform
  1. Windows
Hi,

I would like a VBA that will transpose the data set like this below. I am at a loss of where to start. I am good with VBA for the basic excel functions but I learn by reverse engineering things.

Columns A,B,C is how I am starting, I want to finish with it looking like Columns F and beyond

Tank you

Data.png
 
I added a * to the changed lines in the macro. By the way, jrock8859's macro is quite fast.

VBA Code:
Sub TransposeData_1col()
  Dim dic As Object
  Dim a As Variant, b As Variant
  Dim i As Long, n As Long, lr As Long, y As Long
  Dim nrow As Long, ncol As Long
 
  lr = Range("A" & Rows.Count).End(3).Row
  a = Range("A1:B" & lr).Value              '*
  n = Evaluate("=MAX(COUNTIF(A2:A" & lr & ",A2:A" & lr & "))")
  ReDim b(1 To UBound(a, 1), 1 To n + 1)    '*
 
  Set dic = CreateObject("Scripting.Dictionary")
 
  y = 1
  b(1, 1) = a(1, 1)
  For i = 2 To UBound(a, 1)
    If Not dic.exists(a(i, 1)) Then
      y = y + 1
      dic(a(i, 1)) = y & "|" & 2
      b(y, 1) = a(i, 1)
    End If
   
    nrow = Split(dic(a(i, 1)), "|")(0)
    ncol = Split(dic(a(i, 1)), "|")(1)
   
    b(1, ncol) = a(1, 2)
    'b(1, ncol + 1) = a(1, 3)         '*
    b(nrow, ncol) = a(i, 2)
    'b(nrow, ncol + 1) = a(i, 3)      '*
   
    ncol = ncol + 1                   '*
    dic(a(i, 1)) = nrow & "|" & ncol
   
  Next
 
  Range("F1").Resize(y, UBound(b, 2)).Value = b
End Sub
Thank you. This improves my understanding of the functionality of the vba
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top