VBA 合并连续箭头上固定数量的列

问题描述 投票:0回答:1

我正在尝试编写一个宏,它将在固定的数据范围内合并一行的每个单元格。 对于我的范围的每一行,我需要合并从 b 列到 G 列的单元格。唯一包含数据的单元格是左侧单元格 (Ci)。 我尝试了多种语法,但都无济于事。我不明白我是否应该激活我的工作表,或者我应该如何编写我的变量以使 .merge 方法起作用...如上所述,我尝试了不同数量的语法,但我总是收到错误 1004,应用程序定义或对象定义错误。 这是我的最新版本。

Sub Merge1()

Dim sourcecell As Range
Dim firstcell As Range
Dim lastcell As Range
Dim i As Integer
Dim lastrow As Integer

Sheets("Tabelle1").Activate
Set sourcecell = Range("B2") `cell top left corner
Set firstcell = sourcecell.Offset(i, 2)
Set lastcell = sourcecell.Offset(i, 5)
lastrow = Range("B2").End(xlUp).Row

For i = 14 To lastrow
Range(firstcell, lastcell).Select
    With Selection
        .HorizontalAlignment = xlUp
        .VerticalAlignment = xlLeft
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    Selection.Merge (arrow)
Next i
End Sub
excel vba loops merge sequential
1个回答
0
投票

您需要在循环内设置firstcell和lastcell的值

尝试以下操作:

Dim sourcecell As Range
Dim firstcell As Range
Dim lastcell As Range
Dim i As Long
Dim lastrow As Long

Set sourcecell = Range("B2")
lastrow = Range("B2").End(xlUp).Row

For i = 14 To lastrow
    Set firstcell = sourcecell.Offset(i, 2)
    Set lastcell = sourcecell.Offset(i, 5)
    With Range(firstcell, lastcell)
        .MergeCells = True
    End With
Next i
© www.soinside.com 2019 - 2024. All rights reserved.