0
'MERGE TITLES

ChartLCN = ActiveSheet.Cells(ActiveCell.Offset(0, 0).Row, ActiveSheet.Columns.Count).End(xlToLeft).Column

For mrgst = 0 To ChartLCN

    If ActiveCell.Offset(-1, mrgst) <> "" Then
        mrg = mrgst + 1

        Do While ActiveCell.Offset(-1, mrg) = ""

                 Range(Cells(ActiveCell.Offset(-1, mrgst).Row, ActiveCell.Offset(-1, mrgst).Column), Cells(ActiveCell.Offset(-1, mrg).Row, ActiveCell.Offset(-1, mrg).Column)).Merge
                  mrg = mrg + 1

        Loop
    End If


Next mrgst

你好,stackoverflow和VBA的新手,当我添加这个代码时,我有相当长的一段VBA代码崩溃。我试图用这个循环做的是将两个非空单元格之间的空单元格合并到左侧非空细胞(对不起,不知道怎么说更好)。

|_ _ _ _ _ |_ _ _ _ _|_ _   =>  |........|........|...

(.......Connected to left pipe)

我在代码中使用了 do-loops,它们中的大多数都可以正常工作,当它们崩溃时,我只使用了 for-next 循环。我该怎么做才能使这件作品正常工作并避免 excel 出现白屏和崩溃?

感谢您的时间。

4

1 回答 1

0
Sub Tester()

Dim ChartLCN As Long, mrgst As Long, mrg As Long, col As Long
Dim c As Range, c2 As Range
Dim bDone As Boolean

    ChartLCN = ActiveSheet.Cells(ActiveCell.Row, Columns.Count).End(xlToLeft).Column

    Set c = ActiveCell.Offset(-1, 0)

    Do While Not bDone

        Set c2 = c.End(xlToRight).Offset(0, -1)
        If c2.Column > ChartLCN Then
            Set c2 = ActiveSheet.Cells(c2.Row, ChartLCN)
            bDone = True
        End If
        col = c2.Column + 1

        If c2.Column > c.Column Then ActiveSheet.Range(c, c2).Merge

        Set c = ActiveSheet.Cells(c.Row, col)
    Loop
End Sub
于 2013-06-12T00:02:25.817 回答