Creating a database style list from a matrix/table

Tuttle

New Member
Joined
Aug 3, 2017
Messages
5
Morning all,

I have a matrix of data that I want to turn into a database type listing so I can use pivot tables on the data.

By way of example, I have a table that looks something like this:

WBS ABCWBS XYZWBS LMN
GL Code XYZ$10,000
GL Code ABC$12,000$10,000
GL Code 123$12,300

<tbody>
</tbody>

I need to arrange the data like this

GL Code XYZWBS ABC$10,000
GL Code ABCWBS ABC$12,000
GL Code ABCWBZ XYZ$10,000
GL Code 123WBS LMN$12,300

<tbody>
</tbody>

The actual table I have is 8 rows by 53 columns and changes once a month.

Any tips on how to rearrange the data?

Tuttle.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Maybe a little bit of VBA code...

Here's a before and after


Book1
ABCD
1WBS ABCWBS XYZWBS LMN
2GL Code XYZ$10,000
3GL Code ABC$12,000$10,000
4GL Code 123$12,300
5
6After
7GL Code XYZWBS ABC£10,000
8GL Code ABCWBS ABC£12,000
9GL Code ABCWBS XYZ£10,000
10GL Code 123WBS LMN£12,300
Sheet3


Here's the code I used

Code:
Option Explicit


Sub rearrange()
Dim i As Long
Dim j As Long
Dim counter As Long
counter = 7 ' change to which ever row you want the data to be reconfigured to
For i = 2 To 4 ' change 4 to total rows + 1
    For j = 2 To 4 ' change 4 to total columns + 1
        If Cells(i, j).Value <> "" Then
            Cells(counter, 1) = Cells(i, 1).Value
            Cells(counter, 2) = Cells(1, j).Value
            Cells(counter, 3) = Cells(i, j).Value
            counter = counter + 1
        End If
    Next j
Next i
End Sub

You will need to make adjustments to suit your exact needs.
 
Upvote 0
Thanks - that works well.

How would you adjust the code to drop the output into a different sheet? I have three of these to do in a workbook that is about 20 sheets in size.

Tuttle.
 
Upvote 0
One way to do it might be to bundle the values into an array and dump it in a newly created sheet. Example being...

Code:
Sub rearrangewitharray()
Dim Output() As Variant


counter = 0


ReDim Output(0 To 2, 0 To counter)
For i = 2 To 4
    For j = 2 To 4
        If Cells(i, j).Value <> "" Then
            Output(0, counter) = Cells(i, 1).Value
            Output(1, counter) = Cells(1, j).Value
            Output(2, counter) = Cells(i, j).Value
            counter = counter + 1
            ReDim Preserve Output(0 To 2, 0 To counter)
        End If
    Next j
Next i
Worksheets.Add
ActiveSheet.Cells(1, 1).Resize(UBound(Output, 2), UBound(Output, 1) + 1) = Application.Transpose(Output)
End Sub

This would add a new sheet and insert the data from A1. You could of course skip adding a sheet and reference an existing one.
 
Last edited:
Upvote 0
Can you use something like this. Assume your data is in A1:D4. Place your formulas in A6, B6, and C6. You need to use Cntrl+Shift+Enter for each formula. Copy down.The formula in C6 is a crazy complicated formula, but it is robust.
A6
=INDEX($A$2:$A$4,SMALL(IF($B$2:$D$4<>"",ROW($A$2:$A$4)-ROW($A$2)+1),ROWS($A$6:A6)))

B6
=INDEX($B$1:$D$1,SMALL(IF($B$2:$D$4<>"",ROW($A$2:$A$4)-ROW($A$2)+1),ROWS($B$6:B6)))

C6
=INDEX($B$2:$D$4,INT(SMALL(IF($B$2:$D$4<>"",(ROW($B$2:$D$4)-ROW($B$2)+1)*10^9+COLUMN($B$2:$D$4)-COLUMN($B$2)+1),ROWS($A$6:C6))/10^9),MOD(SMALL(IF($B$2:$D$4<>"",(ROW($B$2:$D$4)-ROW($B$2)+1)*10^9+COLUMN($B$2:$D$4)-COLUMN($B$2)+1),ROWS($A$6:C6)),10^9))

WBS ABCWBS XYZWBS LMN
GL Code XYZ10000
GL Code ABC1200010000
GL Code 12312300
GL Code XYZWBS ABC10000
GL Code ABCWBS XYZ12000
GL Code ABCWBS XYZ10000
GL Code 123WBS LMN12300

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

<tbody>
</tbody>

<tbody>
</tbody>

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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