2013-02-22 116 views
0

我正在研究一个visual basic脚本来解析excel电子表格的特定形式并从中拉取各种数据。我几乎没有VB的经验,所以任何帮助,将不胜感激。visual basic在excel电子表格中解析单元格

如您所见,脚本在电子表格中移动,从各个特定点拉取数据。但最后,在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

的细胞()的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可能会对此进行优化。

+0

谢谢一堆! – 2013-02-27 14:19:43

相关问题