Forum Discussion
Macros and VBA to export data from excel file in specific order into a new excel spreadsheet
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-22 | Feb-23 | Mar-23 | |
| Category | Budget | Budget | Budget |
| A | 10 | 60 | 101 |
| B | 20 | 70 | 201 |
| C | 30 | 80 | 202 |
| D | 40 | 90 | 302 |
| E | 50 | 100 | 400 |
Power BI Data foramt requires:
| Category | Budget | Month | Year |
| A | 10 | December | 2022 |
| B | 20 | December | 2022 |
| C | 30 | December | 2022 |
| D | 40 | December | 2022 |
| E | 50 | December | 2022 |
| A | 60 | February | 2023 |
| B | 70 | February | 2023 |
| C | 80 | February | 2023 |
| D | 90 | February | 2023 |
| E | 100 | February | 2023 |
| A | 101 | March | 2023 |
| B | 201 | March | 2023 |
| C | 202 | March | 2023 |
| D | 302 | March | 2023 |
| E | 400 | March | 2023 |
4 Replies
- NikolinoDEPlatinum Contributor
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 Subuntested, 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.
- shibshibCopper ContributorThank 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- NikolinoDEPlatinum Contributor
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.