2017-10-09 188 views
-1

我有以下輸入:合併重複單元格?

Input

並希望下面的輸出:

Output

預期操作是搜索重複值(列已經被排序)A列。 A中的每個重複值應合併爲1個單元格。另外,合併B中的相同行(如果不同,則取最大值,但假設它們是相同的)。不要觸摸C.

我現在正在做這個手動,這是一個巨大的痛苦。我是VBA新手,但看起來這將是簡單的方法來加速這一點。有小費嗎?

+1

完成合並後,您打算對它們進行排序或過濾嗎?因爲除非所有合併單元格的大小相同,否則您將無法做到這一點。如果你不知道這一點,只是想讓你免於麻煩。我個人大多數時候都是爲了避免合併細胞。 – Tehscript

+0

@Tehscript謝謝。在此步驟之前,我正在進行所有分類和篩選。雖然 – Caprooja

回答

3
Sub MergeCells() 
    'set your data rows here 
    Dim Rows As Integer: Rows = 20 

    Dim First As Integer: First = 1 
    Dim Last As Integer: Last = 0 
    Dim Rng As Range 

    Application.DisplayAlerts = False 
    With ActiveSheet 
     For i = 1 To Rows + 1 
      If .Range("A" & i).Value <> .Range("A" & First).Value Then 
       If i - 1 > First Then 
        Last = i - 1 

        Set Rng = .Range("A" & First, "A" & Last) 
        Rng.MergeCells = True 
        Set Rng = .Range("B" & First, "B" & Last) 
        Rng.MergeCells = True 

       End If 

       First = i 
       Last = 0 
      End If 
     Next i 
    End With 
    Application.DisplayAlerts = True 
End Sub 
+0

這可以像廣告一樣工作。我更新了行數= 20到我的實際數字(以千計),並得到正確的輸出。謝謝你,先生 – Caprooja

2

我這個做了幾次......

Public Sub MergeDuplicates() 

'disable alerts to avoid clicking OK every time it merges 
Application.DisplayAlerts = False 

'define the range 
Dim r As Range 
Set r = Sheets("Sheet1").Range("A1:B4") 

'need a row counter 
Dim i As Long 
i = 1 

'variables to store the value in A in a row and its upstairs neighbor 
Dim this_A As String 
Dim last_A As String 

'step through the rows of the range 
For Each rw In r.Rows 
    If i > 1 Then 'only compare if this is not the first row - nothing to look backwards at! 
     'get the values of A for this row and the one before 
     this_A = rw.Cells(1, 1).Value 
     last_A = rw.Cells(1, 1).Offset(-1, 0).Value 

     'compare this A to the one above; if they are the same, merge the cells in both columns 
     If this_A = last_A Then 
      'merge the cells in column A 
      Sheets("Sheet1").Range(r.Cells(i - 1, 1), r.Cells(i, 1)).Merge 
      'merge the cells in column B 
      Sheets("Sheet1").Range(r.Cells(i - 1, 2), r.Cells(i, 2)).Merge 
     End If 

    End If 

i = i + 1 'increment the counter 

Next rw 

'enable alerts 
Application.DisplayAlerts = True 

End Sub 
+0

可以解決這個問題,但可以在屏幕截圖中解決這個問題,但如果連續有3個或更多副本,則無法合併單元格。出於這個原因,我去了一個不同的答案對不起 – Caprooja

+0

這將工作,如果有任何數量的連續重複!嘗試一下! – Rhys

0

您表示A列進行了排序;在我看來,列A和列B都應該按列A作爲主鍵和列B作爲次要鍵排序。

Option Explicit 

Sub wqwerq() 
    Dim i As Long, d As Long 

    Application.DisplayAlerts = False 

    With Worksheets("sheet3") 
     With .Cells(1, "A").CurrentRegion 
      .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _ 
         Key2:=.Columns(2), Order2:=xlDescending, _ 
         Orientation:=xlTopToBottom, Header:=xlNo 
      For i = .Rows.Count To 1 Step -1 
       If Not .Cells(i, "B").MergeCells Then 
        d = Application.CountIfs(.Columns(1), .Cells(i, "A"), .Columns(2), .Cells(i, "B")) 
        If CBool(d - 1) Then 
         With .Cells(i, "B") 
          .Resize(d, 1).Offset(1 - d, 0).Merge 
          .HorizontalAlignment = xlCenter 
          .VerticalAlignment = xlCenter 
         End With 
        End If 
       End If 
       If i = Application.Match(.Cells(i, "A"), .Columns(1), 0) Then 
        d = Application.CountIfs(.Columns(1), .Cells(i, "A")) 
        If CBool(d - 1) Then 
         With .Cells(i, "A") 
          .Resize(d, 1).Merge 
          .HorizontalAlignment = xlCenter 
          .VerticalAlignment = xlCenter 
         End With 
        End If 
       End If 
      Next i 
     End With 
    End With 

    Application.DisplayAlerts = True 

End Sub