有没有办法改进下面的代码,使其表现更好。我正在尝试在我指定的工作表中查找包含非“薄”边框的所有单元格,并将它们更改为“细线”。< / p>
以下情况有效,但它确实减慢了我的VBA代码。
由于
For Each j In ws.UsedRange.Cells
For k = 5 To 10
If j.Borders(k).Weight <> xlThin Then _
j.Borders(k).Weight = xlThin
Next
Next
我现在尝试了以下,看起来好一点。 Screenupdating已经关闭。有谁认为我可以对性能进行任何进一步的改进???
Set MyUsedRange = ws.UsedRange.Cells
For Each j In MyUsedRange
For k = 5 To 10
If j.Borders(k).Weight <> xlThin Then _
j.Borders(k).Weight = xlThin
Next
Next
答案 0 :(得分:0)
除非我们还没有注意到其他一些代码,否则无需单独设置每个单元格,也无需检查它是否为NOT在设置它之前的东西...
For k = 5 To 10
ws.UsedRange.Borders(k) = xlThin
Next
答案 1 :(得分:0)
你的二手范围非常大吗?
msgbox ws.usedrange.address
试试这段代码:
Dim Rg as Range
set Rg = ws.UsedRange
Application.Screenupdating = False
with ws.Range(Rg.Address) 'Avoids a looping usedRange, in case it's updating itself , might also work by using a simple With Rg
For k = 5 To 10
.Borders(k) = xlThin
Next k
end with
Application.Screeupdating = True