如何合并行中重复的单元格值?我的代码忽略了一些重复值 [英] How to merge duplicate cell values in rows? My code is ignoring some of the duplicate values

查看:50
本文介绍了如何合并行中重复的单元格值?我的代码忽略了一些重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并D列中所有连续的重复单元格。我不关心单元格的格式,也不需要对任何值求和。我想知道下面的代码有什么问题,因为并非所有重复的单元格都在合并.我只能假设我不小心跳过了它们

with thisworkbook.sheets("sheet1") 
For i = StartRow + 1 To LastRow + 1
    
If Cells(i, 4) <> "" Then
    If Cells(i, 4) <> Cells(i - 1, 4) Then
        Application.DisplayAlerts = False
        Range(Cells(i - 2, 4), Cells(StartMerge, 4)).Merge
        Application.DisplayAlerts = True
        StartMerge = i
    End If
End If
Next i
End With

推荐答案

接近您的代码: (更新;删除If Cells(i, 4) <> "" Then)

Sub test1()
    With ThisWorkbook.Sheets("sheet1")
        StartRow = 1
        LastRow = .Cells(.Rows.Count, 4).End(xlUp).Row
        
        StartMerge = StartRow + 1
        Application.DisplayAlerts = False
        For i = StartRow + 1 To LastRow
            If .Cells(i, 4) <> .Cells(i + 1, 4) Then
                .Range(.Cells(StartMerge, 4), .Cells(i, 4)).Merge
                StartMerge = i + 1
            End If
        Next i
        Application.DisplayAlerts = True
    End With
End Sub

结果:

这篇关于如何合并行中重复的单元格值?我的代码忽略了一些重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆