VBA macro to Arrange data in different form

StupidCoffee

New Member
Joined
Jun 11, 2013
Messages
16
I am trying to organize my raw data into a different form chart to help me build a special trend.
I have one sheet with name, date, and shortened names for each component.
As a result of this macro I need a table that shows name, description (which will be repeated), date, and value of the component

Here is a sample

Raw:
NameDate.H.O.N
A3/12/2013123
A5/10/2013456
A6/2/2013789
B1/22/2013101112
B3/15/2013131415
B5/20/2013161718

<colgroup><col><col><col><col span="2"></colgroup><tbody>
</tbody>


Result:
NameDescriptionDateValue
A.HHydrogen3/12/20131
A.OOxygen3/12/20132
A.NNitrogen3/12/20133
A.HHydrogen5/10/20134
A.OOxygen5/10/20135
A.NNitrogen5/10/20136
A.HHydrogen6/2/20137
A.OOxygen6/2/20138
A.NNitrogen6/2/20139
B.HHydrogen1/22/201310
B.OOxygen1/22/201311
B.NNitrogen1/22/201312
B.HHydrogen3/15/201313
B.OOxygen3/15/201314
B.NNitrogen3/15/201315
B.HHydrogen5/20/201316
B.OOxygen5/20/201317
B.NNitrogen5/20/201318

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>


I tried using INDEX function, but I need a macro code for this.

Please help..
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Actually, the description column is not needed
This assumes your data start in A2 and produces a rearranged version (including description) beginning in H2 of the same sheet:
Code:
Sub RearrangeMyMolecules()
Dim lR As Long, R As Range, vA As Variant, Hdrs As Variant, nR As Long, O(1 To _
    4) As Variant, S As Variant, SF As Variant
lR = Range("A" & Rows.Count).End(xlUp).Row
Set R = Range("A2", "E" & lR)
vA = R.Value
Hdrs = Array("Name", "Description", "Date", "Value")
S = Array(".H", ".O", ".N")
SF = Array("Hydrogen", "Oxygen", "Nitrogen")
Range("H1:K1").Value = Hdrs
Application.ScreenUpdating = False
For i = LBound(vA, 1) To UBound(vA, 1)
    For j = 0 To 2
        nR = Range("H" & Rows.Count).End(xlUp).Row + 1
        O(1) = vA(i, 1) & S(j)
        O(2) = SF(j)
        O(3) = vA(i, 2)
        O(4) = vA(i, j + 3)
        Range("H" & nR).Resize(1, 4).Value = O
        Erase O
    Next j
Next i
Columns("H:K").AutoFit
End Sub
 
Upvote 0
Thank you so much!

If I want to produce the result on a new sheet, would I just change the Range("H1:K1") to a desired sheet and range?
 
Upvote 0
Thank you so much!

If I want to produce the result on a new sheet, would I just change the Range("H1:K1") to a desired sheet and range?
This will create a new sheet named "RearrangedData" and put the results on it starting in A1 if you run the code from your raw data sheet. Each time you run the code it will replace the contents of the RearrangedData sheet.
Code:
Sub RearrangeMyMolecules()
'Run this code from the sheet the raw data are on
Dim lR As Long, R As Range, vA As Variant, Hdrs As Variant, nR As Long, O(1 To _
    4) As Variant, S As Variant, SF As Variant, sSht As Worksheet
    
Set sSht = ActiveSheet
lR = sSht.Range("A" & Rows.Count).End(xlUp).Row
Set R = sSht.Range("A2", "E" & lR)
vA = R.Value
Hdrs = Array("Name", "Description", "Date", "Value")
S = Array(".H", ".O", ".N")
SF = Array("Hydrogen", "Oxygen", "Nitrogen")
Application.ScreenUpdating = False
Application.DisplayAlerts = False
On Error Resume Next
Sheets("RearrangedData").Delete
On Error GoTo 0
Sheets.Add after:=sSht
ActiveSheet.Name = "RearrangedData"
With Sheets("RearrangedData")
    .Range("A1:D1").Value = Hdrs
    For i = LBound(vA, 1) To UBound(vA, 1)
        For j = 0 To 2
            nR = .Range("A" & Rows.Count).End(xlUp).Row + 1
            O(1) = vA(i, 1) & S(j)
            O(2) = SF(j)
            O(3) = vA(i, 2)
            O(4) = vA(i, j + 3)
            .Range("A" & nR).Resize(1, 4).Value = O
            Erase O
        Next j
    Next i
    .Columns("A:D").AutoFit
End With
sSht.Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,172
Messages
6,129,290
Members
449,498
Latest member
Lee_ray

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