使用VBA通过变量值动态合并和居中单元格

时间:2019-06-06 14:35:04

标签: excel vba

我正在尝试使用带有VBA的静态起点合并和居中x个单元格。

我的起点始终是Cell D69,并且我总是将D - I列合并在一起,从第69行开始,然后将文本向左对齐。

我当前的宏看起来像这样:

Range("D69:I69").Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.merge
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With

对于第69行x之后的行,我需要继续此过程。我在尝试实现一个使用x作为迭代次数,并且起点为第69行+迭代次数作为行参考点的循环时遇到了困难。

3 个答案:

答案 0 :(得分:1)

  1. 确定最后一行
  2. D to I合并到每一行(从69开始)
  3. 循环完成后,立即格式化整个范围

这还将检查以确保您的最后一行确实大于69,否则将产生错误。


Option Explicit

Sub Merge_For_Client()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1") '<-- Update Sheet
Dim LR As Long, i As Long

LR = ws.Range("D" & ws.Rows.Count).End(xlUp).Row

Application.ScreenUpdating = False
Application.DisplayAlerts = False
    If LR >= 69 Then

        For i = 69 To LR
            ws.Range(ws.Cells(i, "D"), ws.Cells(i, "I")).Merge
        Next i

        With ws.Range(ws.Cells(69, "D"), ws.Cells(LR, "I"))
            .HorizontalAlignment = xlLeft
            'Add any other formats you want to apply in this With Block
        End With

    End If
Application.DisplayAlerts = True
Application.ScreenUpdating = True

End Sub

答案 1 :(得分:0)

查看是否有帮助,请在代码注释中提供更多详细信息:

Sub mergeThis()

Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("SheetName") 'declare and set the worksheet, set your sheet name.
Dim X As Long, howManyTimes As Long 'declare variables to use

howManyTimes = 100 'set how many times here. See additional code for how to get this to last row instead


    For X = 0 To howManyTimes 'loop from 0 to so many times
        With ws.Range("D69:I69").Offset(X) 'use offset from range to get the new range to deal with
            .MergeCells = True
            .HorizontalAlignment = xlLeft
        End With
    Next X
End Sub

我已从合并/格式中删除了所有默认值,如果未将其设置为其他值,则没有任何区别。

如果您需要到达最后一行,那么现在有多种方法可以获取最后一行。只需添加:

Dim lRow As Long: lRow = ws.Cells(Rows.Count, 4).End(xlUp).Row

howManyTimes = lRow

答案 2 :(得分:-1)

假设您的代码工作正常:

这个简单的循环应该可以解决问题

x = 5 'Sample Value

For i = 69 To 69 + x


    Range("D" & i & ":I" & i).Select
        With Selection
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlBottom
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        Selection.Merge
        With Selection
            .HorizontalAlignment = xlLeft
            .VerticalAlignment = xlBottom
            .WrapText = False
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = True
        End With

Next