在Excel VBA中优化一行格式的代码

美好的一天!

现在,这更像是VBA的语法问题,特别是在连续调用多个列时。 我的代码被用来在每次迭代时给一个新行一个格式。 这些迭代逐渐变慢。 我已经研究了一下,find加快速度的方法,我find并实现了一个方法,即Application.ScreenUpdating = False函数,它可以阻止Excel使用相当多的开销。 不过,我认为还有另外一个领域可以进行优化。 下面的代码是相当重复的,并单独调用单独的丑陋和缓慢。 现在,我的问题是,我不知道如何使用Range函数,当我在我的循环中有variables我会减less代码量…? 作为一个新手,我想我只是没有意识到使用范围的正确方法,因为我不断收到错误。

有问题的代码:

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 

这会更快一点:

 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 

如你所见,不需要循环!