Skip to main content

Kutools for Office — 一套工具,五種功能。完成更多工作。

如何根據另一列中的唯一值轉置一列中的儲存格?

Author Xiaoyang Last modified

假設您有一個包含兩列的資料範圍,現在,您想根據另一列中的唯一值將一列中的儲存格轉置為水平行,以獲得以下結果。您有什麼好主意可以在 Excel 中解決這個問題嗎?

A screenshot showing the desired result after transposing cells based on unique values

使用公式根據唯一值轉置一列中的儲存格

使用 VBA 程式碼根據唯一值轉置一列中的儲存格

使用 Kutools for Excel 根據唯一值轉置一列中的儲存格


使用公式根據唯一值轉置一列中的儲存格

使用以下陣列公式,您可以提取唯一值並將其對應的資料轉置為水平行,請按照以下步驟操作:

1. 在空白儲存格(例如 D2)中輸入此陣列公式: =INDEX($A$2:$A$16, MATCH(0, COUNTIF($D$1:$D1, $A$2:$A$16), 0)) ,然後同時按下 Shift + Ctrl + Enter 鍵以獲得正確的結果,請參見截圖:

A screenshot showing the formula to extract unique values for transposing data

注意:在上述公式中,A2:A16 是您要列出唯一值的列,而 D1 是該公式儲存格上方的儲存格。

2. 然後向下拖動填滿控制點以提取所有唯一值,請參見截圖:

A screenshot showing the unique values extracted using a formula

3. 接著在 E2 儲存格中輸入此公式: =IFERROR(INDEX($B$2:$B$16, MATCH(0, COUNTIF($D2:D2,$B$2:$B$16)+IF($A$2:$A$16<>$D2, 1, 0), 0)), 0) ,並記住要按 Shift + Ctrl + Enter 鍵來獲取結果,請參見截圖:

A screenshot showing the formula to transpose cells in Excel based on unique values

注意:在上述公式中,B2:B16 是您要轉置的列資料,A2:A16 是您要基於其轉置值的列,而 D2 包含了在第 1 步中提取的唯一值。

4. 然後向右拖動填滿控制點到您要列出轉置資料的儲存格,直到顯示 0,請參見截圖:

A screenshot showing the transposed data in Excel using formulas

5. 然後繼續向下拖動填滿控制點到儲存格範圍以獲得如以下截圖所示的轉置資料:

A screenshot showing the final transposed data in Excel based on unique values


使用 VBA 程式碼根據唯一值轉置一列中的儲存格

也許這些公式對您來說很複雜,這裡您可以運行以下 VBA 程式碼來獲得所需的結果。

1. 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications 視窗。

2. 點擊 插入 > 模組,然後在模組視窗中貼上以下程式碼。

VBA 程式碼:根據另一列中的唯一值轉置一列中的儲存格:

Sub transposeunique()
'updateby Extendoffice
    Dim xLRow As Long
    Dim i As Long
    Dim xCrit As String
    Dim xCol  As New Collection
    Dim xRg As Range
    Dim xOutRg As Range
    Dim xTxt As String
    Dim xCount As Long
    Dim xVRg As Range
    On Error Resume Next
    xTxt = ActiveWindow.RangeSelection.Address
    Set xRg = Application.InputBox("please select data range(only two columns):", "Kutools for Excel", xTxt, , , , , 8)
    Set xRg = Application.Intersect(xRg, xRg.Worksheet.UsedRange)
    If xRg Is Nothing Then Exit Sub
    If (xRg.Columns.Count <> 2) Or _
       (xRg.Areas.Count > 1) Then
        MsgBox "the used range is only one area with two columns ", , "Kutools for Excel"
        Exit Sub
    End If
    Set xOutRg = Application.InputBox("please select output range(specify one cell):", "Kutools for Excel", xTxt, , , , , 8)
    If xOutRg Is Nothing Then Exit Sub
    Set xOutRg = xOutRg.Range(1)
    xLRow = xRg.Rows.Count
    For i = 2 To xLRow
        xCol.Add xRg.Cells(i, 1).Value, xRg.Cells(i, 1).Value
    Next
    Application.ScreenUpdating = False
    For i = 1 To xCol.Count
        xCrit = xCol.Item(i)
        xOutRg.Offset(i, 0) = xCrit
        xRg.AutoFilter Field:=1, Criteria1:=xCrit
        Set xVRg = xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible)
        If xVRg.Count > xCount Then xCount = xVRg.Count
        xRg.Range("B2:B" & xLRow).SpecialCells(xlCellTypeVisible).Copy
        xOutRg.Offset(i, 1).PasteSpecial Paste:=xlPasteAll, Operation:=xlNone, SkipBlanks:=False, Transpose:=True
        Application.CutCopyMode = False
    Next
    xOutRg = xRg.Cells(1, 1)
    xOutRg.Offset(0, 1).Resize(1, xCount) = xRg.Cells(1, 2)
    xRg.Rows(1).Copy
    xOutRg.Resize(1, xCount + 1).PasteSpecial Paste:=xlPasteFormats
    xRg.AutoFilter
    Application.ScreenUpdating = True
End Sub

3. 然後按 F5 鍵執行此程式碼,會彈出一個提示框提醒您選擇要使用的資料範圍,請參見截圖:

A screenshot of a prompt box for selecting a data range to transpose in Excel

4. 然後點擊 確定 按鈕,另一個提示框會彈出提醒您選擇一個儲存格來放置結果,請參見截圖:

A screenshot of a prompt box for selecting an output cell for transposed data in Excel

6. 點擊 確定 按鈕,並且 B 列中的資料已根據 A 列中的唯一值進行了轉置,請參見截圖:

A screenshot showing the transposed data in Excel after running VBA code


使用 Kutools for Excel 根據唯一值轉置一列中的儲存格

如果您有 Kutools for Excel,結合 高級合併行分割儲存格 功能,您可以快速完成此任務,無需任何公式或程式碼。

Kutools for Excel 提供超過 300 種進階功能,簡化複雜任務,提升創造力與效率。 結合 AI 能力,Kutools 能夠精準自動化任務,讓數據管理變得輕而易舉。Kutools for Excel 的詳細資訊...免費試用...

安裝 Kutools for Excel 後,請按照以下步驟操作:

1. 選擇您要使用的資料範圍。(如果您想保留原始資料,請先將資料複製並粘貼到另一個位置。)

2. 然後點擊 Kutools > 合併與分割 > 高級合併行,請參見截圖:

A screenshot of the Advanced Combine Rows option on the Kutools tab on the Ribbon

3. 在 根據列合併行 對話框中,請進行以下操作:

(1.) 點擊您要基於其轉置資料的列名稱,然後選擇 主鍵

(2.) 點擊另一列,您要轉置的列,然後點擊 合併 並選擇一個分隔符來分隔合併的資料,例如空格、逗號、分號。

A screenshot of the Combine Rows Based on Column dialog box

4. 然後點擊 確定 按鈕,B 列中的資料已經根據 A 列合併到了一個儲存格中,請參見截圖:

A screenshot showing the combined data in Kutools for Excel after merging rows based on unique values

5. 然後選擇合併的儲存格,並點擊 Kutools > 合併與分割 > 分割儲存格,請參見截圖:

A screenshot of the Split Cells option on the Kutools tab on the Ribbon

6. 在 分割儲存格 對話框中,選擇 分割至列類型 選項下,然後選擇分隔您的合併資料的分隔符,請參見截圖:

A screenshot of the Split Cells dialog box

7. 然後點擊 確定 按鈕,並在彈出的對話框中選擇一個儲存格來放置分割結果,請參見截圖:

A screenshot of the dialog box for selecting the output cell

8. 點擊 確定,您將獲得所需結果。請參見截圖:

A screenshot showing the final result of transposed data

立即下載並免費試用 Kutools for Excel!


示範:使用 Kutools for Excel 根據唯一值轉置一列中的儲存格

 
Kutools for Excel:超過 300 種實用工具任您使用!永久免費享受AI功能!立即下載!

最佳 Office 生產力工具

🤖 Kutools AI 助手:以智能執行為基礎,革新數據分析 生成程式碼 創建自訂公式 分析數據並生成圖表 調用 Kutools 增強函數
熱門功能查找、選取項目的背景色或標記重複值刪除空行合併列或單元格且不遺失數據四捨五入(免公式)...
高級 LOOKUP多條件 VLookup多值 VLookup多表查找模糊查找...
高級下拉列表快速創建下拉列表 依賴型下拉列表 多選下拉列表...
列管理器添加指定數量的列移動列切換隱藏列的顯示狀態比較區域及列...
精選功能網格聚焦 設計檢視 增強編輯欄 工作簿及工作表管理器 資源庫(快捷文本) 日期提取器 合併資料 加密/解密儲存格 按列表發送電子郵件 超級篩選 特殊篩選(篩選粗體/傾斜/刪除線...)...
15 大工具集12 項文本工具添加文本刪除特定字符…)50+ 儀表 類型甘特圖等)40+ 實用 公式基於生日計算年齡等)19 項插入工具插入QR码根據路徑插入圖片等)12 項轉換工具金額轉大寫匯率轉換等)7 項合併與分割工具高級合併行分割儲存格等)...及更多
使用 Kutools,語言任你選 — 支援英語、西班牙語、德語、法語、中文及超過40 種語言!

運用 Kutools for Excel,全面提升您的 Excel 技能,體驗前所未有的高效。 Kutools for Excel 提供超過300 項進階功能,讓您提升工作效率、節省時間。 點此尋找您最需要的功能...


Office Tab 為 Office 帶來分頁介面,讓您的工作更加輕鬆簡單

  • 在 Word、Excel、PowerPoint 中啟用分頁編輯與閱讀
  • 在同一視窗的新分頁中打開與創建多份文件,而非開啟新視窗。
  • 提升您的生產力50%,每日可幫您減少數百次鼠標點擊!

所有 Kutools 外掛,一次安裝

Kutools for Office 套裝整合了 Excel、Word、Outlook 和 PowerPoint 的外掛,外加 Office Tab Pro,非常適合需要跨 Office 應用程式協同作業的團隊。

Excel Word Outlook Tabs PowerPoint
  • 全合一套裝 — Excel、Word、Outlook及 PowerPoint 外掛 + Office Tab Pro
  • 一鍵安裝,一份授權 — 幾分鐘完成設置(支援 MSI)
  • 協同運作更順暢 — Office 應用間無縫提升生產力
  • 30 天全功能試用 — 無需註冊、無需信用卡
  • 最超值 — 一次購買,節省單獨外掛費用