Rearranging data for import

Undertegnede

New Member
Joined
Oct 15, 2013
Messages
15
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am looking for a clever way to rearrange data in order to be able to import to a financial system (unit 4 / Agresso).

The input:

Activity201701201702201703201704201705201706201707201708201709201710201711201712
Activity 1
500050005000500050005000500050005000500050005000
Activity 2

3000

4000

2000

1000
Activity 3



6000






<tbody>
</tbody>


Desired output:

Activity 12017015000
Activity 12017025000
Activity 12017035000
Activity 12017045000
Activity 12017055000
Activity 12017065000
Activity 12017075000
Activity 12017085000
Activity 12017095000
Activity 12017105000
Activity 12017115000
Activity 12017125000
Activity 2201701
Activity 2201702
Activity 22017033000
Activity 2201704
Activity 2201705
Activity 22017064000
Activity 2201707
Activity 2201708
Activity 22017092000
Activity 2201710
Activity 2201711
Activity 22017121000
Activity 3201701
Activity 3201702
Activity 3201703
Activity 3201704
Activity 32017056000
Activity 3201706
Activity 3201707
Activity 3201708
Activity 3201709
Activity 3201710
Activity 3201711
Activity 3201712

<tbody>
</tbody>

I would appreciate any input towards a solution.

Best regards.
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Undertegnede,

Welcome to the MrExcel forum.

Here is a macro solution for you to consider that uses two arrays in memory, and, will adjust to the number of raw data rows, and, columns.

I assume that the raw data worksheet, Input, and, the resulting worksheet, Output, already exist.

You can change the worksheet names in the macro.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub RearrangeData()
' hiker95, 09/19/2017, ME1023287
Application.ScreenUpdating = False
Dim wi As Worksheet, wo As Worksheet
Dim a As Variant, i As Long, c As Long
Dim o As Variant, j As Long
Set wi = Sheets("Input")    '<-- you can change the sheet name here
Set wo = Sheets("Output")   '<-- you can change the sheet name here
With wi
  a = wi.Cells(1, 1).CurrentRegion
  ReDim o(1 To UBound(a, 1) * UBound(a, 2), 1 To 3)
End With
For i = 2 To UBound(a, 1) Step 1
  For c = 2 To UBound(a, 2) Step 1
    j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(1, c): o(j, 3) = a(i, c)
  Next c
Next i
With wo
  .UsedRange.ClearContents
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.

Then run the RearrangeData macro.
 
Upvote 0
Code worked perfectly. Many thanks for the quick and precise reply.

Could I also bother you or someone with an addition that will copy a few more columns?
I should be able to do this my self based on your code, but my coding skills are not that great.

Input:

AccountLocationSegmentActivity201701201702201703201704201705201706201707201708201709201710201711201712
71326000100Activity 1100020003000400050006000700080009000100001100012000

<tbody>
</tbody>


Output:
71326000100Activity 12017011000
71326000100Activity 12017012000
71326000100Activity 12017013000
71326000100Activity 12017014000
71326000100Activity 12017015000
71326000100Activity 12017016000
71326000100Activity 12017017000
71326000100Activity 12017018000
71326000100Activity 12017019000
71326000100Activity 120170110000
71326000100Activity 120170111000
71326000100Activity 120170112000

<tbody>
</tbody>
 
Upvote 0
Could I also bother you or someone with an addition that will copy a few more columns?
I should be able to do this my self based on your code, but my coding skills are not that great.

Undertegnede,

Be back in a little while.
 
Upvote 0
Undertegnede,

Here is a new macro solution for you to consider, that is based on your new raw data structure, that uses two arrays in memory, and, will adjust to the number of raw data rows, and, columns.

I assume that the raw data worksheet, Input, and, the resulting worksheet, Output, already exist.

You can change the worksheet names in the macro.

With the same instructions as my reply #2.

Code:
Sub RearrangeData_V2()
' hiker95, 09/19/2017, ME1023287
Application.ScreenUpdating = False
Dim wi As Worksheet, wo As Worksheet
Dim a As Variant, i As Long, c As Long
Dim o As Variant, j As Long
Set wi = Sheets("Input")    '<-- you can change the sheet name here
Set wo = Sheets("Output")   '<-- you can change the sheet name here
With wi
  a = wi.Cells(1, 1).CurrentRegion
  ReDim o(1 To (UBound(a, 1) - 1) * (UBound(a, 2) - 4), 1 To 6)
End With
For i = 2 To UBound(a, 1) Step 1
  For c = 5 To UBound(a, 2) Step 1
    j = j + 1
    o(j, 1) = a(i, 1): o(j, 2) = a(i, 2): o(j, 3) = a(i, 3): o(j, 4) = a(i, 4)
    o(j, 5) = a(1, c): o(j, 6) = a(i, c)
  Next c
Next i
With wo
  .UsedRange.ClearContents
  .Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
  .UsedRange.Columns.AutoFit
  .Activate
End With
Application.ScreenUpdating = True
End Sub

With the same instructions as my reply #2.

Then run the RearrangeData_V2 macro.
 
Last edited:
Upvote 0
Undertegnede,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Undertegnede,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.

Hi again, Hiker95,

It might be a long shot after quite some time, but I was wondering if you (or someone else) could create a code to revert the data from the output-format into the input format?

Best regards,
Arild
 
Upvote 0
I was wondering if you (or someone else) could create a code to revert the data from the output-format into the input format?

Undertegnede,

Please explain why you would want to do the above?
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
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