2013-07-14 94 views
1

我有一個問題,基於一列的相似值合併Excel中的單元格 - 我想保留其他列數據 - 讓我們查看一些截圖,它會更清晰: enter image description here如何基於相似的值合併單元格 - Excel 2010

上面這是數據的初始狀態, 什麼,我想實現的是:

enter image description here

我敢肯定有辦法用VB或formulas-我做需要最簡單的方式,因爲這是針對客戶和它的需要很容易。

謝謝大家的進步。

回答

4
Option Explicit 

Private Sub MergeCells() 
    Application.ScreenUpdating = False 
    Application.DisplayAlerts = False 

    Dim rngMerge As Range, cell As Range 
    Set rngMerge = Range("A1:A100") 'Set the range limits here 

MergeAgain: 
    For Each cell In rngMerge 
     If cell.Value = cell.Offset(1, 0).Value And IsEmpty(cell) = False Then 
      Range(cell, cell.Offset(1, 0)).Merge 
      GoTo MergeAgain 
     End If 
    Next 

    Application.DisplayAlerts = True 
    Application.ScreenUpdating = True 
End Sub 

可以硬編碼範圍限制(即最後一列中的列A到檢查),具有它每次用戶輸入,或以編程方式找到該範圍中的最後一行。無論哪種方式,這應該讓你開始。

順便問一下,你會發現在A列最後一行有以下:

Dim i As Integer 
    i = Range("A1").End(xlDown).Row 
    Set rngMerge = Range("A1:A" & i) 
+0

什麼是擴展這個宏運行在所有列的最佳方式? – Luc

相關問題