0

我正在开发一个可视化的基本脚本来解析特定形式的 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
4

1 回答 1

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

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

于 2013-02-22T17:38:40.897 回答