Fairly complex VBA Code challenge?

zico2000

New Member
Joined
Feb 23, 2017
Messages
6
Hey guys,

I normally would find my own answers by searching the forums and looking at similar questions in the past. However this is a tricky one and I do not really know where to start. I would dearly appreciate any help with my query? ....

I have a spreadsheet, simplified for purposes of demonstrating the problem:

TypeStoreValueFranceGermany
ColourUKRedRougeRot
ShapeUKRoundRondeRund
ColourUKGreenVerteGrun

<tbody>
</tbody>

I am trying to write some VBA code into a Macro that I can run to produce this outcome:

TypeStoreValue
ColourUKRed
FRRouge
DERot
ShapeUKRound
FRRonde
DERund
ColourUKGreen
FRVerte
DEGrun

<tbody>
</tbody>

So basically it loops through each row, inserts two rows underneath and then copies France into the first added row and Germany into the second.

I hope that makes sense and I am more than happy to provide any further clarification. Many thanks in advance.

Yours hopefully,
Paul
 
zico2000,

Here is another macro for you to consider, that is based on your flat text displays, that uses two arrays in memory, and, should be fast.

Sample raw data in the active worksheet:

Excel 2007
ABCDEFGHIJ
1TypeStoreValueFranceGermany
2ColourUKRedRougeRot
3ShapeUKRoundRondeRund
4ColourUKGreenVerteGrun
5
6
7
8
9
10
11

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



And, after the macro in the same worksheet, beginning in the third column to the right of the last used column of the raw data:

Excel 2007
ABCDEFGHIJ
1TypeStoreValueFranceGermanyTypeStoreValue
2ColourUKRedRougeRotColourUKRed
3ShapeUKRoundRondeRundFRRouge
4ColourUKGreenVerteGrunDERot
5ShapeUKRound
6FRRonde
7DERund
8ColourUKGreen
9FRVerte
10DEGrun
11

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



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).

Code:
Sub RearrangeData()
' hiker95, 02/23/2017, ME992758
Dim a As Variant, i As Long, c As Long
Dim o As Variant, j As Long
With ActiveSheet
  a = .Cells(1, 1).CurrentRegion
  ReDim o(1 To (UBound(a, 1) - 1) * (UBound(a, 2) - 2), 1 To 3)
  For i = 2 To UBound(a, 1)
    For c = 3 To UBound(a, 2)
      If c = 3 Then
        j = j + 1: o(j, 1) = a(i, 1): o(j, 2) = a(i, 2)
        o(j, 3) = a(i, c)
      ElseIf a(1, c) = "France" Then
        j = j + 1: o(j, 2) = "FR": o(j, 3) = a(i, c)
      ElseIf a(1, c) = "Germany" Then
        j = j + 1: o(j, 2) = "DE": o(j, 3) = a(i, c)
      End If
    Next c
  Next i
  .Columns(UBound(a, 2) + 3).Resize(, 3).ClearContents
  With .Cells(1, UBound(a, 2) + 3).Resize(, 3)
    .Value = Array("Type", "Store", "Value")
    .Font.Bold = True
  End With
  .Cells(2, UBound(a, 2) + 3).Resize(UBound(o, 1), UBound(o, 2)) = o
  .Columns(UBound(a, 2) + 3).Resize(, 3).AutoFit
End With
End Sub

Then run the RearrangeData macro.

Brilliant thank you.

I now have two great viable options to test and compare for speed. Ultimately this will be working on 1000s of rows with a lot more columns than my simple example spreadsheet.
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Brilliant thank you.

I now have two great viable options to test and compare for speed.

zico2000,

Thanks for the feedback.

You are very welcome. Glad we could help.


Ultimately this will be working on 1000s of rows with a lot more columns than my simple example spreadsheet.

When the time comes, we would probably have to see your actual raw data workbook.
 
Upvote 0

Forum statistics

Threads
1,216,079
Messages
6,128,687
Members
449,464
Latest member
againofsoul

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