簡體   English   中英

Visual Basic來解析Excel電子表格中的單元格

[英]visual basic to parse cells in excel spreadsheet

我正在使用一種視覺基本腳本來解析特定形式的excel電子表格,並從中提取各種數據。 我對VB的經驗很少,因此可以提供任何幫助。

如您所見,腳本在電子表格中移動,從各個特定點提取數據。 但是最后,在“將日記數據輸出到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

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可能會對此進行優化,但在循環內將變量變暗是一個壞主意。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM