设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

使用动态数组完成VBA中的四则运算

1970-1-1 08:00| 发布者: 佚名| 查看: 3136| 评论: 0

[upload=rar]viewFile.asp?ID=65[/upload]使用过大型excel工作表的朋友都深有体会,过多的使用函数会严重降低数据的运算效率.这时候可能会考虑使用VBA .
这个附件中的代码完成的是最普通的四则运算,他比普通的循环要快20倍.
在VBA中使用数组,速度确实是快.

发现改用cells方法后还可以提高一点点速度.
Private Sub CommandButton1_Click()    Dim arr1(), arr2(), arr3()    Dim i As Long, k As Long, h As Single    h = Timer        k = Cells(65536, 1).End(xlUp).Row        ReDim arr1(1 To k - 1, 1 To 1)    arr1 = Range(Cells(2, 1), Cells(k, 1))    ReDim arr2(1 To k - 1, 1 To 1)    arr2 = Range(Cells(2, 2), Cells(k, 2))    ReDim arr3(1 To k - 1, 1 To 1)            For i = 1 To k - 1            arr3(i, 1) = arr1(i, 1) + arr2(i, 1) '根据需要把加号换成别的运算符号        Next i    Range(Cells(2, 3), Cells(k, 3)) = arr3        MsgBox Timer - h & "秒"End Sub


下载地址:http://www.office-cn.net/off/uploads/soft/200610/20061030230958203.rar

最新评论

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-8 01:52 , Processed in 0.066240 second(s), 16 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

返回顶部