Organizing data with VBA

Thisisgeoff

New Member
Joined
Mar 5, 2019
Messages
1
Hello, I'm looking for some help re-organizing some data and am hoping to use VBA to do it. The current macro gets us to this point, but now I'm stuck. What we want to do is change this:

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]A
[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]a
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]b
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]c
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]d1
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]d2
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]d3
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]d4
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]e
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]f
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]g
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]a
[/TD]
[TD]y
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]b
[/TD]
[TD]y
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]c
[/TD]
[TD]y
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]d1
[/TD]
[TD]y
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]e
[/TD]
[TD]y
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]f
[/TD]
[TD]y
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD]g
[/TD]
[TD]y
[/TD]
[/TR]
</tbody>[/TABLE]































To this:

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]a
[/TD]
[TD]b
[/TD]
[TD]c
[/TD]
[TD]e
[/TD]
[TD]f
[/TD]
[TD]g
[/TD]
[TD]d1
[/TD]
[TD]d2
[/TD]
[TD]d3
[/TD]
[TD]d4
[/TD]
[TD]d...
[/TD]
[/TR]
[TR]
[TD]A
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[TD]x
[/TD]
[/TR]
[TR]
[TD]B
[/TD]
[TD]y
[/TD]
[TD]y
[/TD]
[TD]y
[/TD]
[TD]y
[/TD]
[TD]y
[/TD]
[TD]y
[/TD]
[TD]y
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]...
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

There are two caveats, both the number of capital letters and the number of "d" occurances are variable.

Thank you so much for the help!
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try with this

Code:
Sub Organizing_Data()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim data As Range, rng As Range, cell As Range, b As Range
    Dim lr As Double, lc As Double, j As Double
    
    Application.ScreenUpdating = False
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    
    sh2.Cells.ClearContents
    j = 2
    Set data = sh1.Range("A1", sh1.Range("B" & Rows.Count).End(xlUp))
    For Each rng In data.SpecialCells(xlBlanks).Areas
        For Each cell In rng.Offset(-1, 1).Resize(rng.Count + 1)
            sh2.Cells(j, "A").Value = rng.Offset(-1).Cells(1, 1).Value
            Set b = sh2.Rows(1).Find(cell.Value, lookat:=xlWhole)
            If Not b Is Nothing Then
                lr = sh2.Cells(Rows.Count, b.Column).End(xlUp).Row + 1
                sh2.Cells(lr, b.Column).Value = cell.Offset(0, 1).Value
            Else
                lc = sh2.Cells(1, Columns.Count).End(xlToLeft).Column + 1
                sh2.Cells(1, lc).Value = cell.Value
                sh2.Cells(2, lc).Value = cell.Offset(0, 1).Value
            End If
        Next
        j = j + 1
    Next
    Application.ScreenUpdating = True
    
    MsgBox "End"
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,924
Members
452,366
Latest member
TePunaBloke

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