Excel Workbook | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | |||
1 | A | X | A | X | Y | Z | <<< These are Control-Array Formulas | ||||
2 | A | Y | B | X | Z | <<< These are Control-Array Formulas | |||||
3 | A | Z | |||||||||
4 | B | X | |||||||||
5 | B | Z | |||||||||
Sheet1 |
Excel Workbook | ||||
---|---|---|---|---|
A | B | |||
1 | A | X | ||
2 | A | Y | ||
3 | A | Z | ||
4 | B | X | ||
5 | B | Z | ||
6 | ||||
Sheet1 |
Excel Workbook | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
1 | A | X | Y | Z | ||
2 | B | X | Z | |||
3 | ||||||
Results |
Option Explicit
Sub ReorgData()
' hiker95, 03/15/2011
' http://www.mrexcel.com/forum/showthread.php?t=536350
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, SR As Long, ER As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
Set wR = Worksheets("Results")
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
w1.Range("A1:B" & LR).Sort Key1:=w1.Range("A1"), Order1:=xlAscending, Key2:=w1.Range("B1") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
w1.Rows(1).Insert
w1.Range("A1") = "Test"
w1.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wR.Columns(1), Unique:=True
w1.Rows(1).Delete
wR.Rows(1).Delete
LR = wR.Cells(Rows.Count, 1).End(xlUp).Row
For a = 1 To LR Step 1
SR = Application.Match(wR.Cells(a, 1), w1.Columns(1), 0)
ER = Application.Match(wR.Cells(a, 1), w1.Columns(1), 1)
wR.Range("B" & a).Resize(, ER - SR + 1).Value = Application.Transpose(w1.Range("B" & SR & ":B" & ER))
Next a
wR.Activate
Application.ScreenUpdating = True
End Sub
Excel Workbook | ||||
---|---|---|---|---|
A | B | |||
1 | A | X | ||
2 | A | Y | ||
3 | A | Z | ||
4 | B | X | ||
5 | B | Y | ||
6 | B | Z | ||
7 | C | T | ||
8 | C | W | ||
9 | C | x | ||
10 | ||||
Sheet1 |
Excel Workbook | ||||||||
---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | |||
1 | A | X | Y | Z | ||||
2 | B | X | Y | Z | ||||
3 | C | T | W | x | ||||
4 | ||||||||
Results |
Option Explicit
Sub ReorgDataV2()
' hiker95, 03/17/2011
' http://www.mrexcel.com/forum/showthread.php?t=536350
Dim w1 As Worksheet, wR As Worksheet
Dim LR As Long, a As Long, aa As Long, SR As Long, ER As Long, FC As Long
Application.ScreenUpdating = False
Set w1 = Worksheets("Sheet1")
If Not Evaluate("ISREF(Results!A1)") Then Worksheets.Add(After:=w1).Name = "Results"
Set wR = Worksheets("Results")
wR.UsedRange.Clear
Set wR = Worksheets("Results")
LR = w1.Cells(Rows.Count, 1).End(xlUp).Row
w1.Range("A1:B" & LR).Sort Key1:=w1.Range("A1"), Order1:=xlAscending, Key2:=w1.Range("B1") _
, Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:= _
False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal
w1.Rows(1).Insert
w1.Range("A1:B1") = [{"TestA","TestB"}]
w1.Columns(1).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wR.Columns(1), Unique:=True
w1.Columns(2).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=wR.Columns(2), Unique:=True
w1.Rows(1).Delete
LR = wR.Cells(Rows.Count, 2).End(xlUp).Row
wR.Range("B2:B" & LR).Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
wR.Range("A1:B1").ClearContents
wR.Range("B1").Resize(, LR - 2 + 1).Value = Application.Transpose(wR.Range("B2:B" & LR).Value)
wR.Range("B2:B6").ClearContents
LR = wR.Cells(Rows.Count, 1).End(xlUp).Row
For a = 2 To LR Step 1
SR = Application.Match(wR.Cells(a, 1), w1.Columns(1), 0)
ER = Application.Match(wR.Cells(a, 1), w1.Columns(1), 1)
For aa = SR To ER Step 1
FC = Application.Match(w1.Cells(aa, 2), wR.Rows(1), 0)
wR.Cells(a, FC).Value = w1.Cells(aa, 2).Value
Next aa
Next a
wR.Rows(1).Delete
wR.Activate
Application.ScreenUpdating = True
End Sub
wR.Range("B2:B" & LR).Sort Key1:=Range("B2"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal