从Excel缓慢阅读

时间:2013-06-26 10:21:06

标签: excel vba excel-vba vb6

我正在通过VB 6阅读Excel文件,如下所示

Dim objExcel As Excel.Application
Dim objWorkbook As Excel.Workbook
Dim objWorksheet As Excel.Worksheet
Dim ExlUsedRowCount As Long

Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open(FileName, , True)
Set objWorksheet = objWorkbook.Sheets(1)
ExlUsedRowCount = objWorksheet.UsedRange.Rows.Count

For RowIndex = 1 To ExlUsedRowCount
    someVariable = Trim$(objWorksheet.Cells(RowIndex, 9))
Next

objWorkbook.Close False
Set objExcel = Nothing
Set objWorkbook = Nothing
Set objWorksheet = Nothing

我有Excel 2013,这段代码在我的机器上每秒读取270行(),而在客户端(Excel 2010),它只读取33 /秒。我已经看过进程列表,除了EXCEL.EXE之外没有其他进程使用了​​> 80%的CPU。客户端有4台PC就是这种情况。

我尝试将此文件保存为CSV,但是日期列格式为DD / MM,当保存为CSV时,YEAR完全省略。任何线索?

请告知可能出错的地方

1 个答案:

答案 0 :(得分:1)

试试这个:

    Dim objExcel As Excel.Application
    Dim objWorkbook As Excel.Workbook
    Dim objWorksheet As Excel.Worksheet
    Dim lngLoop      As Long
    Dim varArrTemp      as Variant
    Dim rngData         as range

    Set objExcel = CreateObject("Excel.Application")
    Set objWorkbook = objExcel.Workbooks.Open(FileName, , True)
    Set objWorksheet = objWorkbook.Sheets(1)

    Set rngData   = objWorksheet.UsedRange.columns(9)
    varArrtemp =rngData
    For lngLoop = Lbound(varArrTemp) To Ubound(varArrtemp)
        varArrtemp (lngLoop,9)= Trim$( varArrtemp (lngLoop,9))
    Next
    rngData   = varArrTemp

    objWorkbook.Close False
    Set objExcel = Nothing
    Set objWorkbook = Nothing
    Set objWorksheet = Nothing