美好的一天!
现在这更像是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
答案 0 :(得分:2)
这会更快一些:
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
如您所见,不需要循环!