Skip to main content

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

Author: Xiaoyang Last Modified: 2025-05-12

假設您有一個包含兩列的資料範圍,現在,您想根據另一列中的唯一值將一列中的儲存格轉置為水平行,以獲得以下結果。您有什麼好主意可以在 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功能!立即下載!

最佳辦公效率工具

🤖 Kutools AI 助手:基於智能執行方式革新數據分析:智能執行   |  生成代碼  |  創建自訂公式  |  分析數據並生成圖表  |  調用 Kutools 函數
熱門功能查找、標記重複值或識別重複項   |  刪除空行   |  合併列或單元格而不丟失數據   |   四捨五入無需公式 ...
高級 LOOKUP多條件 VLookup    多值 VLookup  |   多表查找   |   模糊查找 ....
高級下拉列表快速創建下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
列管理器添加特定數量的列  |  移動列  |  切換隱藏列的可見狀態  |  比較區域和列 ...
特色功能網格聚焦   |  設計檢視   |   增強編輯欄    工作簿與工作表管理器   |  資源庫(自動文本)   |  日期提取器   |  合併資料   |  加密/解密儲存格    按列表發送電子郵件   |  超級篩選   |   特殊篩選(篩選粗體/斜體/刪除線...) ...
頂級 15 種工具集12 個文本工具添加文本刪除特定字符、...)   |   50+ 圖表 類型甘特圖、...)   |   40+ 實用 公式基於生日計算年齡、...)   |   19 個插入工具插入QR碼根據路徑插入圖片、...)   |   12 個轉換工具金額轉大寫匯率轉換、...)   |   7 個合併與分割工具高級合併行分割儲存格、...)   |   ... 還有更多

使用 Kutools for Excel 提升您的 Excel 技巧,體驗前所未有的高效。 Kutools for Excel 提供超過 300 種高級功能來提高生產力並節省時間。  點擊這裡獲取您最需要的功能...


Office Tab 將標籤式界面帶到 Office,讓您的工作更加輕鬆

  • 在 Word、Excel、PowerPoint、Publisher、Access、Visio 和 Project 中啟用標籤式編輯和閱讀。
  • 在同一窗口的新標籤中打開和創建多個文檔,而不是在新窗口中。
  • 將您的生產力提高 50%,每天為您減少數百次鼠標點擊!