簡體   English   中英

讀取文件夾中的所有文本文件,檢查是否匹配並將文本文件值插入Excel工作表

[英]Read from all text files in folder, check for matches and insert text file value into Excel sheet

我正在嘗試使用一個代碼,該代碼將允許我檢查文件夾中所有文本文件的兩行。

每個文本文件的結構如下:

NS1234        <--- A random reference number on the first line
Approve       < Reject or Approve on the second line

目前,代碼僅讀取一個我指定了名稱的文本文件,但是我希望它掃描所有.txt文件。

接下來,當我打開電子表格時,我將進行以下設置:

Column A     Column
NS1234 

我希望我的代碼掃描所有文本文件,以針對所有文本文件檢查A列中是否有任何匹配的參考號。

然后在找到匹配項的地方,將“ Approve”或“ Reject”(寫在文本文件的第二行)插入到s列中的相應行中

碼:

Public Sub test()
    Dim fn As Integer
    fn = FreeFile
    Open "Z:\NS\Approval\NS32D1QR.txt" For Input As fn

    Dim wholeFile As String
    wholeFile = Input(LOF(fn), #fn)

    Close #fn

    Dim splitArray
    splitArray = Split(wholeFile, vbCrLf)

    Dim lineNum As Integer
    lineNum = 2


    Dim i As Integer, intValueToFind As Integer
    intValueToFind = NS32D1QR
    For i = 1 To 500    ' Revise the 500 to include all of your values
        If Cells(i, 1).Value = intValueToFind And splitArray(lineNum - 1) = "Approve" Then
    Range("S" & ActiveCell.Row).Value = "Approve"
    End If

    Next i



End Sub

我不確定您在循環中進行的測試,但是在我看來,信息在第一行的第二行中,因此沒有用到循環或在其中使用特殊變量。 讓我知道這是否正常! ;)

這是一個要測試的子項,因為它是一個函數,您可以在其上循環或在Excel工作簿中使用它。

Sub test()

With Sheets("Sheet1")
    For i = 2 To .Rows(.Rows.Count).End(xlUp).Row
        .Cells(i, "S") = Get_AorP(.Cells(i, "A"))
    Next i
End With

End Sub

這是您想要做的,轉換為一個函數:

    Public Function Get_AorP(ByVal Value_to_Find As String) As String
        Dim fn As Integer, _
            Txts_Folder_Path As String, _
            File_Name As String, _
            wholeFile As String, _
            splitArray() As String, _
            i As Integer

    On Error GoTo ErrHandler
    Txts_Folder_Path = "Z:\NS\Approval\"
    File_Name = Dir(Txts_Folder_Path & "*.txt")

    While File_Name <> vbNullString
        fn = FreeFile
        Open Txts_Folder_Path & File_Name For Input As fn
            wholeFile = Input(LOF(fn), #fn)
        Close #fn
        MsgBox File_Name
        splitArray = Split(wholeFile, vbCrLf)

        If UBound(splitArray) < 2 Or LBound(splitArray) > 1 Then
            'avoid empty text files
        Else
            If InStr(1, splitArray(0), Value_to_Find) <> 0 Then
                If InStr(1, splitArray(1), "Approve") Then
                    Get_AorP = "Approve"
                    Exit Function
                Else
                    If InStr(1, splitArray(1), "Reject") Then
                        Get_AorP = "Reject"
                        Exit Function
                    Else
                        'Nothing to do
                    End If
                End If
            Else
                'not the good value
            End If
        End If
        File_Name = Dir()
    Wend

    Get_AorP = "No matches found"
    Exit Function
ErrHandler:
        Get_AorP = "Error during the import." & vbCrLf & Err.Number & " : " & Err.Description
    End Function

暫無
暫無

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

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