VBA-读取文件的几种方法用时对比

不同方法的其他优劣可以参考其余文档,本文仅对比用时,并且由于文件以其他方式读取过会影响下次读取速度,本次结果仅供参考.

本人小白,欢迎指正.

方法用时(s)备注
workbook.open0.14-0.39
execut.Excel1.58
GetObeject0.28,0.25,0.01,0.02后台打开
Application1.429
Sub WorkbooksOpenRoy()
'#方法1:workbookOpen
starttime = Timer
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    Dim rFilePath, rFileName, rSheetName, rRowMax
    rFilePath = "C:\Users\roy.luo\Base\"
    rFileName = "ProcessName.xlsx"
    rSheetName = "Sheet1"
    
    Workbooks.Open rFilePath & rFileName, UpdateLinks:=False, ReadOnly:=True, Password:="001"
    With Workbooks(rFileName).Sheets(rSheetName)
        rRowMax = .Cells(.Rows.Count, 2).End(xlUp).Row
        aRaw = .Range("A1:C" & rRowMax)
    End With
    Workbooks("" & rFileName).Close False
    
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
timecum = Timer - starttime
End Sub
Sub ExecuteRoy()
'#方法2:Execute
starttime = Timer
    bkpath = "C:\Users\roy.luo\Base\"
    bkname = "ProcessName.xlsx"
    sheetName = "Sheet1"
    cellname1 = "A1"
    cellname2 = "C140"
    trr = rAds(0, cellname1, cellname2)
    ReDim arr(1 To trr(0, 1), 1 To trr(0, 2))
    i = 1
    For R = trr(1, 1) To trr(2, 1)
        j = 1
        For C = trr(1, 2) To trr(2, 2)
            FullName = "'" & bkpath & "\[" & bkname & "]" & sheetName & "'!" & "R" & R & "C" & C
            arr(i, j) = ExecuteExcel4Macro(FullName)
            j = j + 1
        Next
     i = i + 1
    Next
TimeCsm = Timer - starttime
End Sub
Sub GetObjectRoy()
'#方法3:GetObject
starttime = Timer
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Dim Wb As Workbook
    Dim Temp As String
    rFilePath = "C:\Users\roy.luo\Base\"
    rFileName = "ProcessName.xlsx"
    rSheetName = "Sheet1"
    
    Set Wb = GetObject(rFilePath & rFileName)
        With Wb.Sheets(rSheetName)
          rRowMax = .Cells(.Rows.Count, 2).End(xlUp).Row
          aRaw = .Range("A1:C" & rRowMax)
        End With
    Set Wb = Nothing
     
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
timecum = Timer - starttime
End Sub
Sub ApplicationRoy()
'#方法4:Application
starttime = Timer
    Application.DisplayAlerts = False
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Dim myApp As New Application
    Dim Sh As Worksheet
    Dim Temp As String
     rFilePath = "C:\Users\roy.luo\Base\"
    rFileName = "ProcessName.xlsx"
    rSheetName = "Sheet1"
    myApp.Visible = False
    Set Sh = myApp.Workbooks.Open(rFilePath & rFileName).Sheets(rSheetName)
    With Sh
        rRowMax = .Cells(.Rows.Count, 2).End(xlUp).Row
        aRaw = .Range("A1:C" & rRowMax)
    End With
    myApp.Quit
    Set Sh = Nothing
    Set myApp = Nothing
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
timecum = Timer - starttime
End Sub

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值