VBA Excel - 单元格合并缓慢 - 多个工作表上的多个列

时间:2017-04-25 18:25:31

标签: excel vba excel-vba

下面的代码将多个工作表中的单元格合并到多个工作表上,而且它很慢!

我希望能得到任何帮助以加快速度。

不幸的是,我无法解决必须合并单元格的事实。

我打破了for语句 - 它有点帮助,但它仍然非常非常慢。

新手在这里。温柔:)谢谢!!!谢丽尔

Function MergeCells()

    Dim WS_Count As Integer
    Dim i As Integer
    Dim ShName As String

    WS_Count = ActiveWorkbook.Worksheets.Count

    PIS.Activate

    For i = 1 To WS_Count
        Select Case ActiveWorkbook.Worksheets(i).Name
            Case "Med Curr", "Med Ren", "Med RevRen", "Med Prop", "Med Renewal Alts A", "Med Renewal Alts B", _
                "Med Renewal Alts C", "Med Prop Other Markets 1A", "Med Prop Other Markets 1B", "Med Prop Other Markets 1C", _
                "Med Prop Other Markets 2A", "Med Prop Other Markets 2B", "Med Prop Other Markets 2C", _
                "Med Prop Other Markets 3A", "Med Prop Other Markets 3B", "Med Prop Other Markets 3C"
                    ActiveWorkbook.Worksheets(i).Activate
                    ShName = ActiveSheet.Name
                    MergeCellsx (ShName)
        End Select
    Next i

End Function

Function MergeCellsx(ShName)

    Dim RngB As Range
    Dim row As Integer

    With Sheets(ShName)
        For row = 6 To 12
            Set RngB = .Range(.Cells(row, 2), .Cells(row, 3))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 5), .Cells(row, 6))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 8), .Cells(row, 9))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

        Next row
        For row = 14 To 18
            Set RngB = .Range(.Cells(row, 2), .Cells(row, 3))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 5), .Cells(row, 6))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 8), .Cells(row, 9))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

        Next row
        For row = 20 To 22
            Set RngB = .Range(.Cells(row, 2), .Cells(row, 3))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 5), .Cells(row, 6))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 8), .Cells(row, 9))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

        Next row
        For row = 24 To 25
            Set RngB = .Range(.Cells(row, 2), .Cells(row, 3))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 5), .Cells(row, 6))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 8), .Cells(row, 9))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

        Next row
        For row = 27 To 34
            Set RngB = .Range(.Cells(row, 2), .Cells(row, 3))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 5), .Cells(row, 6))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

            Set RngB = .Range(.Cells(row, 8), .Cells(row, 9))
            RngB.Merge
            RngB.HorizontalAlignment = xlCenter

        Next row
    End With
End Function

0 个答案:

没有答案