合并单元格但跳过空单元格

时间:2018-04-17 16:06:50

标签: excel vba

我正在尝试合并多列中的单元格并跳过空白单元格。如果它们不为空,我能够将单元格中的值合并并对齐到中心。但是,如果单元格为空,则脚本不会抛出任何错误,但根本不执行任何操作。

以下是我的脚本中合并和对齐的部分:

Sub MergeAndCenterCells()

Dim wb As Workbook
Dim ws As Worksheet
Set wb = ActiveWorkbook
Set ws = wb.Sheets("Sheet1")
Dim varTestVal As Variant
Dim intRowCount As Integer
Dim intAdjustment As Integer


ws.Range("A3").Select

While Selection.Offset(1, 0).Value <> ""
    intRowCount = 1
    varTestVal = Selection.Value
    While Selection.Offset(1, 0).Value = varTestVal
        intRowCount = intRowCount + 1
        Selection.Offset(1, 0).Select
        Selection.ClearContents
    Wend
    intAdjustment = (intRowCount * -1) + 1
    Selection.Offset(intAdjustment, 0).Select
    Selection.Resize(intRowCount, 1).Select
    With Selection
        .Merge
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    Selection.Offset(1, 0).Resize(1, 1).Select
Wend
End Sub

如何使其适用于A列中为空的单元格?感谢

0 个答案:

没有答案