请问大家用VBA对EXECL表格编程,运行起来很慢!

因为用户归档的运行数据无法转换成标准的EXECL表,我把数据输入到EXECL表中,用VBA宏测试了颜色、列宽度及数据格式,请问为什么运行起来这么慢?为什么单独运行修改颜色的脚本,却很快,修改列宽我看到一个一个在刷新,有没有更快的办法?谢谢!
脚本如下:
Sub 颜色自动显示()
Dim i, j, Obj
For i = 4 To 31
For j = 10 To 500 Step 4
Set Obj = Cells(i, j)
If Not IsNull(Cells(i, j).Value) And IsNumeric(Cells(i, j).Value) Then
   Cells(i, j) = Format(Cells(i, j), "0.00")
   If Abs(Cells(i, j).Value) > 1 Then
     If Abs(Cells(i, j).Value – 1) <= 0.2 Then
       Cells(i, j).Interior.Color = RGB(255, 255, 0)
       Else
       Cells(i, j).Interior.Color = RGB(255, 0, 0)
       End If
   Else
       Cells(i, j).Interior.Color = RGB(0, 255, 0)
       End If
Else
       Cells(i, j).Interior.Color = RGB(255, 250, 250)
End If

Cells(i, j – 2).ColumnWidth = 7
With Obj ‘对象多个属性操作
.ColumnWidth = 5.22
End With

Next
Next
End Sub

最佳答案

用.Range代替.Cells,速度会提高很多。
前者是区域,后者是单元格。
比如操作第4行到第31行的第10列,可以这样表达:
.Range(.Cells(4, 10), .Cells(31, 10))

如果是整个数据区域(从第4行第10列,到第31行第500列),则是:
.Range(.Cells(4, 10), .Cells(31, 500))

提问者对于答案的评价:
确实这样,但是赋值的时候都是一个一个赋值的啊?很慢,这不可能啊?那IT那么大的数据量怎么搞的?谢谢!

原创文章,作者:more0621,如若转载,请注明出处:https://www.zhaoplc.com/plc315073.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2020年11月1日
下一篇 2020年11月1日

相关推荐