【问题标题】:Optimizing code for the formatting of a row in Excel VBA优化Excel VBA中行格式的代码
【发布时间】:2015-01-21 17:02:33
【问题描述】:

美好的一天!

现在这更像是 VBA 的语法问题,特别是在调用一行中的多个列时。我的代码用于在每次迭代时为新行提供格式。这些迭代变得越来越慢。我已经四处寻找加速它的方法,我发现并实现了一个 Application.ScreenUpdating = False 函数,它可以阻止 Excel 使用相当大的开销。但是,我认为还有另一个领域可以优化。以下代码相当重复,并且单独调用单元格,既丑陋又慢。现在,我的问题是我不知道如何使用 Range 函数,当我在循环中有变量 i 时,它会减少代码量......?作为一个新手,我想我只是不知道使用 Range 的正确方法,因为我不断收到错误。

有问题的代码:

Dim i As Long
Dim EndRow As Long

EndRow = ThisWorkbook.Worksheets("Flags").Cells(Rows.Count, 1).End(xlUp).Row

For i = 19 To EndRow + 1 Step 1

        ThisWorkbook.Worksheets("Flags").Rows(i).RowHeight = 45
        ThisWorkbook.Worksheets("Flags").Cells(i, 1).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 1).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 2).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 2).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 3).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 3).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 4).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 4).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 5).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 5).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 6).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 6).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 7).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 7).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 8).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 8).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 9).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 9).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 10).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 10).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 11).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 11).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 12).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 12).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 13).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 13).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 14).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 14).Borders.Weight = xlMedium
        ThisWorkbook.Worksheets("Flags").Cells(i, 15).Borders.LineStyle = xlContinuous
        ThisWorkbook.Worksheets("Flags").Cells(i, 15).Borders.Weight = xlMedium

    Next

【问题讨论】:

  • 迁移到 Code Review SE 的可能候选人?

标签: vba excel optimization


【解决方案1】:

这样会快一点:

Sub dural()
    Dim EndRow As Long
    EndRow = ThisWorkbook.Worksheets("Flags").Cells(Rows.Count, 1).End(xlUp).Row
    Range("A1:A" & EndRow).EntireRow.RowHeight = 45
    With Range(Cells(1, 1), Cells(EndRow, 15))
        .Borders.LineStyle = xlContinuous
        .Borders.Weight = xlMedium
    End With
End Sub

如您所见,不需要循环!

【讨论】:

  • 这段代码没有调用正确的单元格,可能是因为你没有遵守 .Cells(ROWIndex, COLUMNIndex) 表示法。它也不允许通过链接命令按钮进行多次迭代,而原始按钮做得很好。暂时只能保留原版,然后继续做其他事情。谢谢你的回答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多