I am trying to match data like this (as an example):
[TABLE="class: grid, width: 500"]
Suburb 1
[/td]Suburb 2
[/td]Apples
[/td]Oranges
[/td]Bananas
[/td]A
[/td]F
[/td]2
[/td]6
[/td]7
[/td]C
[/td]E
[/td]4
[/td]3
[/td]4
[/td]B
[/td]D
[/td]5
[/td]4
[/td]3
[/td]D
[/td]C
[/td]4
[/td]5
[/td]7
[/td]
[/TABLE]
to this, so that the data from apples, oranges and bananas shifts WITH suburb 2 to match up with suburb 1.
[TABLE="class: grid, width: 500"]
Suburb 1
[/td]Suburb 2
[/td]Apples
[/td]Oranges
[/td]Bananas
[/td]A
[/td]C
[/td]C
[/td]4
[/td]5
[/td]7
[/td]B
[/td]D
[/td]D
[/td]5
[/td]4
[/td]3
[/td]
[/TABLE]
I have attached a spreadsheet. I can't figure out what the VBA code would be.
In the spreadsheet I want to match Column E with Column B and shift E:O to align with B. This code is matching E with A and moving A, C and D. I need A, C and D to stay where they are and match E with B and move E:O.
[ATTACH=CONFIG]64325[/ATTACH]
Code
Sub test()
Dim SLA, i As Long, ii As Long, o, x
SLA = Sheets("Sheet12").Cells(1).CurrentRegion.Value
ReDim o(1 To UBound(SLA, 2))
With CreateObject("Scripting.Dictionary")
.CompareMode = 1
For i = 2 To UBound(SLA, 1)
o(1) = SLA(i, 1): .Item(SLA(i, 1)) = o
Next
For i = 2 To UBound(SLA, 1)
If .exists(SLA(i, 3)) Then
o = .Item(SLA(i, 3))
Else
ReDim o(1 To UBound(SLA, 2))
End If
For ii = 2 To UBound(SLA, 2)
o(ii) = SLA(i, ii)
Next
.Item(SLA(i, 3)) = o
Next
x = Application.Transpose(Application.Transpose(.items))
End With
With Sheets.Add.Cells(1).Resize(, UBound(x, 2))
.Value = SLA
.Offset(1).Resize(UBound(x, 1)).Value = x
With .CurrentRegion
.Columns.AutoFit
.Rows.AutoFit
End With
End With
End Sub
Display More