Code fails on “Application.Match” call, “object re

2019-08-31 01:41发布

问题:

Need help, in this line....If IsError(Application.Match(iLine, arr, 0)) Then ... Its runtime error is that an object is required. I've tried a number of different things to solve, but am stuck. Been researching but not finding anything as a resolution.

When I try to declare..."Dim arr as variant" it retorts that "Expected end of statement". I am simply clicking on "ProcessCollection.vbs" to run the script. I'm using EditPlus3, and it does highlight "Application" in red text in this line.

I've tried VbsEdit and when it debugs to this line, iLine, arr, are defined, but the value of "Application" is "Empty".

GetFiles()
'WriteCSV()

'*****
Function GetFiles
Dim arr
'Dim arr as Variant
Dim iFileLines
Dim iLine
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\hgis\a"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files

For Each objFile in colFiles
    'Wscript.Echo objFile.Name
    mfn = objStartFolder +"\"+ objFile.Name
    Wscript.Echo mfn
    'open file & process each file
    set fso = CreateObject("Scripting.FileSystemObject") 
    Set theFile = fso.OpenTextFile(mfn, 8, True) 
    iFileLines = theFile.Line
    iLine = 0
    arr = Array(2, 3, iFileLines - 1) ' second, third, and 2nd from last
    msgbox iFileLines

    Set objFileToRead = CreateObject("Scripting.FileSystemObject").OpenTextFile(mfn,1)
    Dim strLine, TotStr
    TotStr = "CellA"
    do while not objFileToRead.AtEndOfStream
         strLine = objFileToRead.ReadLine()
         msgbox strline
         'Parse lines for specific data - i.e. "-2014-" to get date/time stamp
         iLine = iLine + 1
         'And then check if the line number is in the array:
         'Capture data and store to csv file for Excel analysis
         If IsError(Application.Match(iLine, arr, 0)) Then
            ' It isn't in the array, do this....
            msgbox "no"
         Else
            ' It is in the array, grab it
            TotStr = TotStr +","+ strline 
            msgbox TotStr
         End If
    loop
    objFileToRead.Close
    Set objFileToRead = Nothing
Next

'csvFile.Close 
End Function
'*****

回答1:

I found a function from Justin Doles at DigitalDeviation.com that I used to replace the Application.Match function.

Function IsInArray(strIn, arrCheck)
    'IsInArray: Checks for a value inside an array
    'Author: Justin Doles - www.DigitalDeviation.com
    Dim bFlag 
    bFlag = False
    If IsArray(arrCheck) AND Not IsNull(strIn) Then
        Dim i
        For i = 0 to UBound(arrCheck)
            If LCase(arrcheck(i)) = LCase(strIn) Then
                bFlag = True
                Exit For
            End If
        Next
    End If
    IsInArray = bFlag
End Function
'*****