[英]Creating a VBA macro that creates a new consolidated workbook from columns of another excel data-set
[英]Creating a consolidated set of pairs on Excel
我正在處理一個 Excel 文件,該文件具有不同國家之間各種運輸產品的進出口數據,它看起來很像這樣:
目標是創建一個合並的狀態對,它們之間有貿易關系。 因此,上述示例的最終列表應如下所示:
解決這個問題的最佳方法是什么?
因此,假設您的數據在第一個屏幕截圖中的 a&b 中,將此公式放在 c =a1&b1
列中,然后按 Enter,然后向下拖動右下角,以便該公式適用於該列中的所有單元格。 然后復制該列並將其粘貼為值。
然后在 d 列中,使用公式 =countifs(c:c,c1) 並以相同方式將其向下拖動。 現在將 d 列粘貼為值。
最后,在數據選項卡上,刪除基於 c 列的重復項,然后刪除 c 列。
您可以使用 Windows Excel 2010+ 和 O365 中的 Power Query 執行此操作
M碼
let
Source = Excel.CurrentWorkbook(){[Name="Table3"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Importing Country", type text}, {"Exporting Country", type text}}),
#"Added Custom" = Table.AddColumn(#"Changed Type", "Country", each List.Sort({[Importing Country],[Exporting Country]})),
#"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Importing Country", "Exporting Country"}),
#"Extracted Values" = Table.TransformColumns(#"Removed Columns", {"Country", each Text.Combine(List.Transform(_, Text.From), ";"), type text}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Extracted Values", "Country", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), {"Country.1", "Country.2"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Country.1", type text}, {"Country.2", type text}}),
#"Grouped Rows" = Table.Group(#"Changed Type1", {"Country.1", "Country.2"}, {{"Occurrences", each Table.RowCount(_), Int64.Type}})
in
#"Grouped Rows"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.