合并单元格时出现 VBA 错误 1004

合并单元格时出现 VBA 错误 1004

我不知道如何使用 VBA 编程,我正在尝试合并 Excel 表中每 13 个单元格重复一次的单元格。我能够找到下面的代码,但当我应用它时,我收到错误“运行时错误‘1004’:应用程序定义或对象定义错误”,当我调试时,我得到指示,问题出在 .Merge 行中

有人能帮我吗??

Sub Test()
TitleRow = 1 'if title contain more than one row, change the value 1 to the actual number of rows'
i = 0
Application.DisplayAlerts = False
Do
    Set StartCell = ActiveSheet.Range("B" & (TitleRow + 13 * i + 1))
    Set EndCell = ActiveSheet.Range("B" & (TitleRow + 13 * i + 13))
    With ActiveSheet.Range(StartCell, EndCell)
        .Merge
        .VerticalAlignment = xlCenter
        .HorizontalAlignment = xlCenter
    End With
    i = i + 1
    Loop Until Range("B" & (TitleRow + 13 * i) + 1) = ""
    Application.DisplayAlerts = True
End Sub

相关内容