Excel - 将单元格转置为具有配对数据的行
Excel - Transpose a Cell into Rows with Paired Data
我有一个具有以下布局的 CSV 导出文件:
ID
Section A
Section B
...
1
val1, val2
val3, val4, val5
...
2
val6, val7, val8
val9, val10
...
我想将数据集转换为以下输出:
ID
Section
Value
1
Section A
val1
1
Section A
val2
1
Section B
val3
1
Section B
val4
1
Section B
val5
2
Section A
val6
2
Section A
val7
2
Section A
val8
2
Section B
val9
2
Section B
val10
...
...
...
我的挑战是我必须处理成千上万的行,所以我希望能够对整个数据集应用相同的解决方案。
我想出了如何将逗号分隔值转置为多行,但似乎无法弄清楚如何以编程方式将转置值与相应的 ID
和 Section
配对。
下面是我的 VBA 转置代码,以防有人想看:
Sub TransposeTest()
Dim rng As Range
Dim inputRng As Range, outputRng As Range
titleTxt = "Transpose Test"
Set inputRng = Application.Selection.Range("A1")
Set inputRng = Application.InputBox("Input cell:", titleTxt, inputRng.Address, Type:=8)
Set outputRng = Application.InputBox("Output cell:", titleTxt, Type:=8)
output = VBA.Split(inputRng.Range("A1").Value, ",")
outputRng.Resize(UBound(output) - LBound(output) + 1).Value = Application.Transpose(output)
End Sub
您可以受益于数组和拆分以轻松转换数据:
Sub test()
Dim i As Long, j As Long, k As Long, s As Long
Dim MyData As Variant
Dim SubData As Variant
Dim TotalColumns As Long
MyData = Range("A1").CurrentRegion.Value
TotalColumns = Range("A1").CurrentRegion.Columns.Count
Range("E1").Value = "ID"
Range("F1").Value = "Section"
Range("G1").Value = "Value"
k = 2 'initial row to paste data
For i = 2 To UBound(MyData) Step 1
For j = 2 To TotalColumns Step 1 'j=2 because first column because it's ID
SubData = Split(MyData(i, j), ", ")
For s = 0 To UBound(SubData) Step 1
Range("E" & k).Value = MyData(i, 1) 'ID always in first column
Range("F" & k).Value = MyData(1, j) 'Section always in first row
Range("G" & k).Value = SubData(s)
k = k + 1
Next s
Next j
Next i
Erase SubData
Erase MyData
End Sub
我在同一个工作表中得到了输出,但可以很容易地调整代码以使其在不同的工作表中。
如果您的 table 中的 9 个单元格配置为名为 'Data' 的 Excel table,则下面的 M 代码:
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Section A", type text}, {"Section B", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Section A", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section A"),
#"Split Column by Delimiter1" = Table.ExpandListColumn(Table.TransformColumns(#"Split Column by Delimiter", {{"Section B", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section B"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Split Column by Delimiter1", {"ID"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Section"}}),
#"Trimmed Text" = Table.TransformColumns(#"Renamed Columns",{{"Value", Text.Trim, type text}}),
#"Removed Duplicates" = Table.Distinct(#"Trimmed Text")
in
#"Removed Duplicates"
将在 PowerQuery 中呈现此结果
(您需要为每个部分设置 按分隔符拆分列 步骤)
我有一个具有以下布局的 CSV 导出文件:
ID | Section A | Section B | ... |
---|---|---|---|
1 | val1, val2 | val3, val4, val5 | ... |
2 | val6, val7, val8 | val9, val10 | ... |
我想将数据集转换为以下输出:
ID | Section | Value |
---|---|---|
1 | Section A | val1 |
1 | Section A | val2 |
1 | Section B | val3 |
1 | Section B | val4 |
1 | Section B | val5 |
2 | Section A | val6 |
2 | Section A | val7 |
2 | Section A | val8 |
2 | Section B | val9 |
2 | Section B | val10 |
... | ... | ... |
我的挑战是我必须处理成千上万的行,所以我希望能够对整个数据集应用相同的解决方案。
我想出了如何将逗号分隔值转置为多行,但似乎无法弄清楚如何以编程方式将转置值与相应的 ID
和 Section
配对。
下面是我的 VBA 转置代码,以防有人想看:
Sub TransposeTest()
Dim rng As Range
Dim inputRng As Range, outputRng As Range
titleTxt = "Transpose Test"
Set inputRng = Application.Selection.Range("A1")
Set inputRng = Application.InputBox("Input cell:", titleTxt, inputRng.Address, Type:=8)
Set outputRng = Application.InputBox("Output cell:", titleTxt, Type:=8)
output = VBA.Split(inputRng.Range("A1").Value, ",")
outputRng.Resize(UBound(output) - LBound(output) + 1).Value = Application.Transpose(output)
End Sub
您可以受益于数组和拆分以轻松转换数据:
Sub test()
Dim i As Long, j As Long, k As Long, s As Long
Dim MyData As Variant
Dim SubData As Variant
Dim TotalColumns As Long
MyData = Range("A1").CurrentRegion.Value
TotalColumns = Range("A1").CurrentRegion.Columns.Count
Range("E1").Value = "ID"
Range("F1").Value = "Section"
Range("G1").Value = "Value"
k = 2 'initial row to paste data
For i = 2 To UBound(MyData) Step 1
For j = 2 To TotalColumns Step 1 'j=2 because first column because it's ID
SubData = Split(MyData(i, j), ", ")
For s = 0 To UBound(SubData) Step 1
Range("E" & k).Value = MyData(i, 1) 'ID always in first column
Range("F" & k).Value = MyData(1, j) 'Section always in first row
Range("G" & k).Value = SubData(s)
k = k + 1
Next s
Next j
Next i
Erase SubData
Erase MyData
End Sub
我在同一个工作表中得到了输出,但可以很容易地调整代码以使其在不同的工作表中。
如果您的 table 中的 9 个单元格配置为名为 'Data' 的 Excel table,则下面的 M 代码:
let
Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Section A", type text}, {"Section B", type text}}),
#"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Changed Type", {{"Section A", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section A"),
#"Split Column by Delimiter1" = Table.ExpandListColumn(Table.TransformColumns(#"Split Column by Delimiter", {{"Section B", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Section B"),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Split Column by Delimiter1", {"ID"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"Attribute", "Section"}}),
#"Trimmed Text" = Table.TransformColumns(#"Renamed Columns",{{"Value", Text.Trim, type text}}),
#"Removed Duplicates" = Table.Distinct(#"Trimmed Text")
in
#"Removed Duplicates"
将在 PowerQuery 中呈现此结果