私は、特定の形式のExcelスプレッドシートを解析し、そこからさまざまなデータを取得するためのVisualBasicスクリプトに取り組んでいます。私は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