用于解析excel电子表格中的单元格的visual basic

时间:2013-02-22 16:47:53

标签: excel vbscript

我正在开发一个可视化的基本脚本,用于解析特定形式的Excel电子表格并从中提取各种数据。我对VB的经验很少,所以任何帮助都会受到赞赏。

如您所见,脚本在电子表格中移动,从各个特定点提取数据。但最后,在Pull Journal Data out For循环中,我在命令提示符中收到以下错误。

test parse(92, 3) Microsoft VBScript runtime error: Unknown runtime error.

抛出此错误的代码:

rem Pull Journal Information out alone
For x = 7 to UsedRowsCount
    For y = 0 to 5
        If y= 0 Then
            WScript.Echo vbCRLF
        End If
        dim word
        word = objExcel.Cells(x,y).value
        WScript.Echo word
    Next
Next

我很沮丧,因为我之前使用非常相似的循环没有任何问题。如上面的循环:

rem Report Total Aggregate Data for range covered
WScript.Echo "Total from all Journals by Month"
For i = 6 to UsedColumnsCount
    WScript.Echo Cells(5,i).value
    Wscript.Echo Cells(6,i).value
Next

任何帮助将不胜感激。我复制下面的整个杂乱的脚本:

set objExcel = CreateObject("Excel.Application")
set objWorkbook = objExcel.Workbooks.Open _ 
    ("E:\Focus\Internship\Code\VB\emerald_jr1_2012.xls")


dim Sheet
set Sheet = objExcel.ActiveWorkbook.Worksheets(1)
usedColumnsCount = Sheet.UsedRange.Columns.Count
usedRowsCount = Sheet.UsedRange.Rows.Count

dim Cells
set Cells = Sheet.Cells


REM Test to see if it's a jr1
if Cells(1,1).Value = "Journal Report 1 (R3)" then
    WScript.Echo "File is a jr1 report."
else
    WScript.Echo "File is NOT a recognized report"
end if


rem Test to determine how many months are covered in the report and what year
dim lastMonth
select case usedColumnsCount
    Case 9
        lastMonth = "only"
    Case 10
        lastMonth = "through Feb"
    Case 11
        lastMonth = "through Mar"
    Case 12
        lastMonth = "through Apr"
    Case 13
        lastMonth = "through May"
    Case 14
        lastMonth = "through Jun"
    Case 15
        lastMonth = "through Jul"
    Case 16
        lastMonth = "through Aug"
    Case 17
        lastMonth = "through Sep"
    Case 18
        lastMonth = "through Oct"
    Case 19
        lastMonth = "through Nov"
    Case 20
        lastMonth = "through Dec"
    Case Else
        lastMonth = "uncertain"
end select
WScript.Echo "Report holds values for Jan " & lastMonth


rem Using VBS built-in support of Regular expressions to parse the year indicated in a particular row: the first date header.
dim dateYear
dateYear = Cells(5,6).value
rem The dates come out in the script formated as 1/1/2012
rem WScript.Echo dateYear

set re = New RegExp
re.Pattern = ".*?((?:(?:[1]{1}\d{1}\d{1}\d{1})|(?:[2]{1}\d{3})))(?![\d])"
Set matches = re.Execute(dateYear)
If matches.Count > 0 Then
    Set match = matches(0)
    If match.Submatches.Count > 0 Then
        For i = 0 to match.SubMatches.Count-1
            WScript.Echo "Report covers year " & match.Submatches(i)
        Next
        End If
Else
    WScript.Echo "Error, year data not found"
End If

rem Report Total Aggregate Data for range covered
WScript.Echo "Total from all Journals by Month"
For i = 6 to UsedColumnsCount
    WScript.Echo Cells(5,i).value
    Wscript.Echo Cells(6,i).value
Next

rem Pull Journal Information out alone
For x = 7 to UsedRowsCount
    For y = 0 to 5
        If y= 0 Then
            WScript.Echo vbCRLF
        End If
        dim word
        word = Cells(x,y).value
        WScript.Echo word
    Next
Next

objExcel.Workbooks(1).Close
objExcel.Quit

set Sheet = Nothing
set objExcel = Nothing

1 个答案:

答案 0 :(得分:1)

Cells()的ColumnIndex参数的有效范围从1开始:

我想你打算写:

rem Pull Journal Information out alone

For x = 7 to UsedRowsCount

    For y = 0 to 5

        If y= 0 Then

            WScript.Echo vbCRLF

        Else

            WScript.Echo objExcel.Cells(x,y).Value

        End If

    Next y

Next x

除非最后一个值在其他地方使用,否则您不需要 word 变量。尽管Excel可能会对此进行优化,但在循环中调暗变量是一个坏主意。

相关问题