VBA to transpose table and split column into two

megatracer

New Member
Joined
Jan 21, 2014
Messages
4
Hello all,

I am struggling to transpose the table, plus I would need to split the newly rotated column into two.

Here is an example:

WorkweekIntern - maleIntern - femaleIntern - other
WW39210
WW40100

<tbody>
</tbody>


Here is the transposed table:


type/genderWW39WW40
Intern - male21
Intern - female10
Intern - other00

<tbody>
</tbody>


And the final step - dividing first column into two. THe splitter is " - "


typegenderWW39WW40
Internmale21
Internfemale10
Internother00

<tbody>
</tbody>

Table should be copied from one permanent tab, let´s call it "Source" into the tab called "Converted data".

:confused:
 
Last edited:

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG28Sep50
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, Sp [COLOR="Navy"]As[/COLOR] Variant
Application.ScreenUpdating = False
Range("A1").CurrentRegion.Copy
[COLOR="Navy"]With[/COLOR] Sheets("Coverted Data")
    .Range("A1").PasteSpecial Transpose:=True
    .Columns(2).Insert
    .Range("A1:B1").Value = Array("Type", "Gender")
    [COLOR="Navy"]Set[/COLOR] Rng = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]End[/COLOR] With
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    Sp = Split(Dn.Value, "-")
    Dn = Sp(0): Dn.Offset(, 1) = Sp(1)
[COLOR="Navy"]Next[/COLOR] Dn
Application.ScreenUpdating = True
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Hello Mick,

I am getting a runtime error 1004: Application defined or object defined error

I commented everything else besides .Range("A1").PasteSpecial Transpose:=True
and the error is there
 
Upvote 0
I think the error is mine , because I have called your sheet "Coverted Data" instead on Converted Data"
alter line below:-
Code:
[COLOR=#000080]With[/COLOR] Sheets("Converted Data")
 
Upvote 0
Hi Mick,

this one I saw and corrected before.. but still the error was there.

in the meantime I was able to adapt one of the existing scripts to rotate the table using slightly different technique.
Differently to what I showed before as resulting table, I am turning work weeks into an attribute (column) of headcount, similar to gender and type.

Code:
    Application.ScreenUpdating = False    rw = 2
    Set sh_input = Sheets("Source")
    a = sh_input.UsedRange.Value
    Set sh = Sheets("Converted Data")
    sh.Cells.Clear
    With sh
        .Cells(1, 1).Resize(1, 4) = Array("Type", "Gender", "WW", "HC")
        For i = 2 To UBound(a)
            For j = 2 To 4
                If a(i, j) <> "" Then
                    
                         Sp = Split(a(1, j), " - ")
                        .Cells(rw, 1) = Sp
                        .Cells(rw, 2) = Sp(1)
                        .Cells(rw, 3) = a(i, 1)
                        .Cells(rw, 4) = a(i, j)
                     rw = rw + 1
                End If
            Next
        Next
        For i = 1 To 4
            .UsedRange.Borders(i).LineStyle = xlContinuous
        Next
    End With
    
    Application.ScreenUpdating = True


Thanks anyway for your quick help!!
 
Upvote 0

Forum statistics

Threads
1,215,503
Messages
6,125,175
Members
449,212
Latest member
kenmaldonado

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