VBA to transpose dataset

zach9208

Board Regular
Joined
Dec 15, 2015
Messages
117
Hello,

I am looking for help with a macro to change the format of a file to transpose data so it is going down versus going across. I need this to be a loop for each record I have.

fv4grR

The following link provides an image of dummy data both the before and after formatting https://ibb.co/fv4grR . As you can see, I need the EST_MND_USD_AMT and ANULZ_RVNU_AMT fields to be going down instead of across. the values in F2:J2 are the values that need to be repeated for each record in the EST_FNDG_QTR_NM column. I also need the data to be copied down for each of the 5 new rows created for each quarter. I am okay with moving these to a different tab if needed. The current tab is called "Global Funds".


Let me know if you need any clarifications. Thanks in advance for the help!!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I do not think I can use this for the project I am working on. I am working on a macro that is cleaning up template files that need to be in a specific format to load into a database. Therefore, I have to repeat this function on 20+ workbooks per cycle. I really need to use VBA. Here is what I have thus far (I know it is rough code), just trying to make due.




Code:
Sub UpdateGlobalFunds2()


    Dim DSht As Worksheet
    Dim Rng As Range
    Dim Fll As Long
    
Application.ScreenUpdating = False


Worksheets.Add().Name = "DatabaseFormat"
'Range("H3:T3").Select 'Moves quarter labels down one row
   ' Selection.Cut
   ' Range("H4").Select
   ' ActiveSheet.Paste
        


    Set DSht = Sheets("DatabaseFormat")
    
    With Sheets("Global Funds")
        For Each Rng In .Range("A3", .Range("A" & Rows.Count).End(xlUp))
            Rng.Resize(, 7).Copy DSht.Range("A" & Rows.Count).End(xlUp).Offset(1).Resize(6, 7)
            Rng.Offset(, 7).Resize(, 6).Copy
            DSht.Range("H" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues, Transpose:=True
        Next Rng
    End With
    
    
    
    Columns("A:B").Delete Shift:=xlToLeft
    
    Columns("F:F").Insert Shift:=xlToRight
    
    Range("A1").Value = "CALC_BPS_AMT"
    Range("B1").Value = "PRMY_PRDCT_NM"
    Range("C1").Value = "BTQ_NM"
    Range("D1").Value = "MJR_INV_VCHL_NM"
    Range("E1").Value = "PTF_CD"
    Range("F1").Value = "EST_FDNG_QTR_NM"
    Range("F2").Value = "=Index!$A$6"
    Range("F3").Value = "=Index!$A$7"
    Range("F4").Value = "=Index!$A$8"
    Range("F5").Value = "=Index!$A$9"
    Range("F6").Value = "=Index!$A$10"
    Range("F7").Value = "=Index!$A$11"
    Range("G1").Value = "EST_MNDT_USD_AMT"
    
 With DSht
    Fll = Range("B" & Rows.Count).End(xlUp).Row - 6
        Range("F2:F7").Copy Range("F8").Resize(Fll)
 End With


Application.CutCopyMode = False
End Sub


Hello, you can use the unpivot feature of Power Query to achieve this...




I am having issues getting the following code copying my range of F2:F7, to the end of the data. For some reason it only does this one more time.


Code:
With DSht
    Fll = Range("B" & Rows.Count).End(xlUp).Row - 6
        Range("F2:F7").Copy Range("F8").Resize(Fll)
 End With




Lastly, I still have not figured out how to pivot the ANULZ_RVNU_AMT columns without doubling up the number of rows with the first set of pivoted rows for EST_MND_USD_AMT.
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,560
Members
449,237
Latest member
Chase S

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