Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在不循环Excel的情况下合并重复单元格的最快方法_Excel_Vba_Merge_Duplicates_Disjoint Union - Fatal编程技术网

在不循环Excel的情况下合并重复单元格的最快方法

在不循环Excel的情况下合并重复单元格的最快方法,excel,vba,merge,duplicates,disjoint-union,Excel,Vba,Merge,Duplicates,Disjoint Union,我想快速合并包含重复值的单元格。该表如下所示: 重复的单元格范围可能是不连续的或非相邻的单元格。我想要一种快速识别重复区域并合并它们而不使用For循环的方法。[不知道,但我认为可能有一种最快的创新方法,可以不使用循环,使用Excel数组公式和VBA代码的组合,来选择和合并重复的单元格范围。] 顺便说一句,上面的代码工作正常,直到它在第.Merge行出现以下错误 编辑 这是监视窗口的快照,显示arr内容以及R.Address 输出: 不需要任何选择,这只是为了演示: 输出应如下所示: 编

我想快速合并包含重复值的单元格。该表如下所示:

重复的单元格范围可能是不连续的或非相邻的单元格。我想要一种快速识别重复区域并合并它们而不使用For循环的方法。[不知道,但我认为可能有一种最快的创新方法,可以不使用循环,使用Excel数组公式和VBA代码的组合,来选择和合并重复的单元格范围。]

顺便说一句,上面的代码工作正常,直到它在第.Merge行出现以下错误

编辑 这是监视窗口的快照,显示arr内容以及R.Address

输出: 不需要任何选择,这只是为了演示:

输出应如下所示:

编辑… 假设行中的重复值相同?因此,只需合并重复的列值。必须有一种快速、创新的方式来进行合并

最终输出图像:

问题是,您的代码只能找到两个相邻的单元格,而没有使用此代码查找第三个:
设置R=.Range(.cells(I,J),.cells(I-1,J))

在第一个循环之后,它添加这两个单元格

在另一个循环之后,它添加接下来的2个单元格

这会导致重叠

您可以在选择的较暗阴影处看到

我刚刚用注释编辑了代码的一部分,这样您就可以看到它是如何完成的。但我相信仍有改进的空间

Sub MergeCellsNew()
    Application.DisplayAlerts = False
    Dim n As Name
    Dim fc As FormatCondition
    Dim Rng As Range, R As Range
    Dim lRow As Long
    Dim I&, J&
    Dim arr As Variant

    ReDim arr(1 To 1) As Variant

    With ThisWorkbook.Sheets("tst")
        Set Rng = .Range("A2:D11")
        lRow = Rng.End(xlDown).Row

        For J = 1 To 4
            I = 2 'I = Rng.Row   to automatically start at the first row of Rng
            Do While I <= lRow
                Set R = .Cells(I, J) 'remember start cell

                'run this loop as long as duplicates found next to the start cell
                Do While Trim(UCase(.Cells(I, J))) = Trim(UCase(.Cells(I + 1, J)))
                    Set R = R.Resize(R.Rows.Count + 1) 'and resize R + 1
                    I = I + 1
                Loop

                'now if R is bigger than one cell there are duplicates we want to add to the arr
                'this way single cells are not added to the arr
                If R.Rows.Count > 1 Then
                    arr(UBound(arr)) = R.Address
                    ReDim Preserve arr(1 To UBound(arr) + 1)
                End If
                I = I + 1
            Loop
        Next J
        ReDim Preserve arr(1 To UBound(arr) - 1)

        Set R = .Range(Join(arr, ","))
        With R
            .Merge
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With

        Stop
    End With

    Application.DisplayAlerts = True
End Sub
Sub-MergeCellsNew()
Application.DisplayAlerts=False
以n为名称
作为格式化条件的Dim fc
变暗Rng作为范围,R作为范围
暗淡的光线和长的一样
Dim I&J&
作为变体的Dim-arr
ReDim arr(1到1)作为变型
使用此工作簿。工作表(“tst”)
设置Rng=.Range(“A2:D11”)
lRow=Rng.End(xlDown).Row
对于J=1到4
I=2'I=Rng.行自动从Rng的第一行开始

请尽管做,我会告诉你你想要的结果。你所说的“合并”是什么意思?对我来说,合并意味着A2:D2成为一个单元格。当它出错时,
arr
是什么?(顺便说一句,合并单元格是个坏消息。)这将需要许多循环。这是不可能的。@ScottCraner我已经编辑了文章并添加了输出图像。@ScottCraner-根据快速测试,情况似乎不是这样。我在两个表上都运行了您的代码。第一个表-无错误。第二个表的range.range(“A16:D29”)-I再次在Set R=.range(Join(arr,“,”)上出现了相同的应用程序定义错误。当然,如果您更改
Set Rng=.range(“A2:D11”)
,则
I
必须相应地更改
Rng
的第一行。您可以通过使用
I=Rng.Row
而不是
I=2
来自动执行此操作。我已将
Set Rng=.Range(“A2:I11”)
调整为
Set Rng=.Range(“A2:I12061”)
并将
I=Rng.Row
设置为
I=Rng.Row,但仍会得到sifar在@PEH时的相同错误
Sub MergeCellsNew()
    Application.DisplayAlerts = False
    Dim n As Name
    Dim fc As FormatCondition
    Dim Rng As Range, R As Range
    Dim lRow As Long
    Dim I&, J&
    Dim arr As Variant

    ReDim arr(1 To 1) As Variant

    With ThisWorkbook.Sheets("tst")
        Set Rng = .Range("A2:D11")
        lRow = Rng.End(xlDown).Row

        For J = 1 To 4
            I = 2 'I = Rng.Row   to automatically start at the first row of Rng
            Do While I <= lRow
                Set R = .Cells(I, J) 'remember start cell

                'run this loop as long as duplicates found next to the start cell
                Do While Trim(UCase(.Cells(I, J))) = Trim(UCase(.Cells(I + 1, J)))
                    Set R = R.Resize(R.Rows.Count + 1) 'and resize R + 1
                    I = I + 1
                Loop

                'now if R is bigger than one cell there are duplicates we want to add to the arr
                'this way single cells are not added to the arr
                If R.Rows.Count > 1 Then
                    arr(UBound(arr)) = R.Address
                    ReDim Preserve arr(1 To UBound(arr) + 1)
                End If
                I = I + 1
            Loop
        Next J
        ReDim Preserve arr(1 To UBound(arr) - 1)

        Set R = .Range(Join(arr, ","))
        With R
            .Merge
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
        End With

        Stop
    End With

    Application.DisplayAlerts = True
End Sub