excel 合并各种不同的单元格

uxhixvfz  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(203)

我需要合并不同的单元格数量。像一个显示在图像顶部的一个勾号(参考下面所附的图像链接)
我有数百个excel文件,每个文件都有数千行数据,有谁能帮我生成这个问题的vba代码吗?

我尝试了这个方法,但合并的单元格只有空的,预期是合并数据,如图所示,空单元格

Sub MergeColumnCells()
    Dim lastRow As Long
    Dim i As Long

    'Specify the column you want to merge
    Dim col As Integer
    col = 1 'Change this to the column number you want to merge

    lastRow = ActiveSheet.Cells(Rows.Count, col).End(xlUp).Row

    For i = 2 To lastRow
        If Cells(i, col).Value = Cells(i - 1, col).Value Then
            Range(Cells(i - 1, col), Cells(i, col)).Merge
        End If
    Next i
End Sub
bq3bfh9z

bq3bfh9z1#

Option Explicit

Sub MergeColumnCells()
   Dim n As Long, i As Long, lastrow As Long
   n = 1
   Application.ScreenUpdating = False
   With ActiveSheet
       lastrow = .Cells(.Rows.Count, "C").End(xlUp).Row
       For i = 2 To lastrow
          If .Cells(i, "A") = "" Then
              n = n + 1
          ElseIf n > 1 Then
              .Cells(i - n, "A").Resize(n).Merge
              n = 1
          End If
       Next
       ' last group
       If n > 1 Then
           .Cells(i - n, "A").Resize(n).Merge
       End If
   End With
   Application.ScreenUpdating = True
   MsgBox "Done"
End Sub

相关问题