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:

A
a
x
b
x
c
x
d1
x
d2
x
d3
x
d4
x
e
x
f
x
g
x
B
a
y
b
y
c
y
d1
y
e
y
f
y
g
y

<tbody>
</tbody>































To this:

a
b
c
e
f
g
d1
d2
d3
d4
d...
A
x
x
x
x
x
x
x
x
x
x
x
B
y
y
y
y
y
y
y
...

<tbody>
</tbody>

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

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
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,215,001
Messages
6,122,648
Members
449,092
Latest member
peppernaut

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