Macros and VBA to export data from excel file in specific order into a new excel spreadsheet

Copper Contributor

Greetings,

I need to rearrange my data to link it to my PowerBI Dashboard.
I was wondering if you could advise me how to do it? I have 75 spreadsheets with 177 rows each. Is there any way to automate the process with Macros and VBA?  
Current data format:

 Dec-22Feb-23Mar-23
CategoryBudgetBudgetBudget
A1060101
B2070201
C3080202
D4090302
E50100400

Power BI Data foramt requires:

CategoryBudgetMonthYear
A10December2022
B20December2022
C30December2022
D40December2022
E50December2022
A60February2023
B70February2023
C80February2023
D90February2023
E100February2023
A101March2023
B201March2023
C202March2023
D302March2023
E400March2023

 

 

 

4 Replies

@shibshib 

Here some information on how to export data from excel file in a specific order into a new excel spreadsheet using Macros and VBA.

 

Sub ExportData()
    Dim ws As Worksheet
    Dim wb As Workbook
    Dim rng As Range
    Dim r As Long
    Dim c As Long
    Dim cat As String
    Dim bud As Double
    Dim mon As String
    Dim yr As Long
    
    'Change this to your source workbook name
    Set wb = Workbooks("Source.xlsx")
    
    'Change this to your destination workbook name
    Workbooks.Add.SaveAs "Destination.xlsx"
    
    'Loop through all worksheets in source workbook
    For Each ws In wb.Worksheets
    
        'Change this to your data range address
        Set rng = ws.Range("A1:D6")
        
        'Loop through all rows and columns in data range
        For r = 3 To rng.Rows.Count
            For c = 2 To rng.Columns.Count
            
                'Get category, budget, month and year values from each cell
                cat = rng.Cells(r, 1).Value
                bud = rng.Cells(r, c).Value
                
                Select Case Left(rng.Cells(1, c).Value, 3)
                    Case "Dec"
                        mon = "December"
                        yr = Right(rng.Cells(1, c).Value, 4)
                    Case "Feb"
                        mon = "February"
                        yr = Right(rng.Cells(1, c).Value, 4)
                    Case "Mar"
                        mon = "March"
                        yr = Right(rng.Cells(1, c).Value, 4)
                    'Add more cases for other months if needed
                    
                End Select
                
                'Write the values to destination workbook in required format
                
                With Workbooks("Destination.xlsx").Worksheets(1)
                
                    .Cells(.Rows.Count, 1).End(xlUp).Offset(1) = cat
                    
                    .Cells(.Rows.Count, 2).End(xlUp).Offset(1) = bud
                    
                    .Cells(.Rows.Count, 3).End(xlUp).Offset(1) = mon
                    
                    .Cells(.Rows.Count, 4).End(xlUp).Offset(1) = yr
                    
                End With
                
            Next c
            
        Next r
        
        'Save and close destination workbook
        
        Workbooks("Destination.xlsx").Close SaveChanges:=True
    Next ws
   
End Sub

 

untested, without guarantee.

 

This code will loop through all the worksheets in your source workbook and extract the category, budget, month and year values from each cell.

Then it will write them to a new workbook in the required format.

You can change the workbook names, data range address and month cases as per your needs.

 

I hope this helps you with your task.

 

Thank you, it doesn't show month and year. The output shows as below:

A 10 0
A 60 0
A 101 0
B 20 0
B 70 0
B 201 0
C 30 0
C 80 0
C 202 0
D 40 0
D 90 0
D 302 0

@shibshib 

You can add the following line of code to include the month and year in the output:

mon = Format(rng.Cells(1, c).Value, "mmmm")

yr = Format(rng.Cells(1, c).Value, "yyyy")

This will format the date in the cell to show the month and year in the output.

You can replace the Case statement with the above code to get the desired output.

 

Thank you so much for your help. Unfortunately, I couldn't make it work so I found another solution which was much easier, so I created custom function in Power Query.