将选定日期的选定数据从文本复制到excel

rur96b6h  于 2023-01-27  发布在  其他
关注(0)|答案(1)|浏览(242)

我能够使用下面提到的代码将文本文件中的数据复制到Excel文件中。但是,我想问一下,我们是否可以在将文本文件中的数据复制到Excel中时添加以下选项。
1.如果我们可以添加一个选项,我们可以在代码中写入文本文件名。原因是在FolderLocation中有多个文本文件,我无法选择需要从中复制数据的特定文本文件。
1.目前它从文本文件中复制所有数据,有没有办法我们可以添加一个条件或日期选项的代码,而不是选择所有的数据,它可以选择某些日期的数据。文本文件中的数据如下所述
[03]2023年1月7日星期六10:10:58-初始化
[03]2023年1月7日星期六10:10:58-选定密钥
[03]2023年1月7日星期六10:10:58-主持人
[03]2023年1月7日星期六10:10:58-服务器
[03]2023年1月7日星期六10:10:58-客户
[07]2023年1月10日星期二06:51:02-嘘
[08]2023年1月10日星期二06:51:02-嘘
03]1月10日星期二06:51:02-
[07]1月10日星期二06:51:02-
日志文件中的数据是多个日期的,我希望我们是否可以复制某些日期的数据,例如,如果我在代码中写入"07Jan23"日期,它只复制07Jan23的所有完整行。
子导入文本文件数据到Excel()

Dim fileLocation As String, textData As String

Dim rowNum As Long

folderLocation = "E:\Logs"

Set fso = CreateObject("Scripting.FileSystemObject")

Set folder = fso.GetFolder(folderLocation)

rowNum = 1

Close #1

For Each textFile In folder.Files

    fileLocation = folder & "\" & textFile.Name

    Open fileLocation For Input As #1

    Do While Not EOF(1)

        Line Input #1, textData

        textData = Replace(textData, ";", ",")

        If InStr(textData, ",") = 0 Then

            Cells(rowNum, 1) = textData

        Else

            tArray = Split(textData, ",")

            nColumn = 1

            For Each element In tArray

单元格(行数,n列)=元素

nColumn = nColumn + 1

            Next element

        End If

        rowNum = rowNum + 1

    Loop

    Close #1

Next textFile

末端子组件
我会一直感激

xriantvc

xriantvc1#

Sub ImportTextFileDatatoExcel()

    Const LOGS = "E:\Logs"
    Const DBUG = False ' True for debug messages
    
    Dim wb As Workbook, ws As Worksheet
    Dim fso As Object, ts As Object, folder As Object, f As Object
    Dim dtFirst As Date, dtLast As Date, dt As Date
    Dim arFile, arLine, v, yy As String, mmm As String, dd As String
    Dim n As Long, i As Long, r As Long, c As Long, s As String
    
    s = InputBox("Enter Start Date dd/mm/yyyy", "Start Date", Format(Date, "dd/mm/yyyy"))
    If IsDate(s) Then
        dtFirst = CDate(s)
    Else
        MsgBox s & " is not a valid date", vbCritical
        Exit Sub
    End If
     
    s = InputBox("Enter End Date dd/mm/yyyy", "End Date", Format(dtFirst, "dd/mm/yyyy"))
    If IsDate(s) Then
        dtLast = CDate(s)
    Else
        MsgBox s & " is not a valid date", vbCritical
        Exit Sub
    End If
    
    s = "From " & Format(dtFirst, "dd mmm yyyy") & " to " & Format(dtLast, "dd mmm yyyy")
    If vbNo = MsgBox(s, vbYesNo, "Confirm Yes/No") Then
         Exit Sub
    End If
    
    ' start scanning logs
    Set wb = ThisWorkbook
    Set ws = wb.ActiveSheet
    ws.Cells.ClearContents
    r = 2
    
    ' select files
    Dim arLogs
    With Application.FileDialog(msoFileDialogFilePicker)
        .InitialFileName = LOGS
        .AllowMultiSelect = True
        .Filters.Add "Log files or Text", "*.log; *.txt, 1"
        .Show
        n = .SelectedItems.Count
        If n = 0 Then Exit Sub
        ReDim arLogs(1 To n)
        For i = 1 To n
            arLogs(i) = .SelectedItems(i)
        Next
    End With
    
    ' scan files
    Set fso = CreateObject("Scripting.FileSystemObject")
    For n = 1 To UBound(arLogs)
        Set f = fso.getFile(arLogs(n))
                
        ' read in file
        If DBUG Then Debug.Print f.Name
        Set ts = f.OpenAsTextStream(1, -2) ' read, default encoding
        s = ts.readall
        ts.Close
        
        ' scan each line
        arFile = Split(s, vbCrLf)
        For Each v In arFile
        
            ' convert 10Jan23 to 10-Jan-23
            s = Mid(CStr(v), 10, 7)
            dd = Left(s, 2)
            mmm = Mid(s, 3, 3)
            yy = Right(s, 2)
            s = dd & "-" & mmm & "-" & yy
           
            ' check valid date
            If IsDate(s) Then
                dt = CDate(s)
                If (dt >= dtFirst) And (dt <= dtLast) Then
                
                    ' split line into columns
                    arLine = Split(CStr(v), ";")
                    c = 1 + UBound(arLine)
                    ws.Cells(r, 1).Resize(, c) = arLine
                    r = r + 1
                    
                    If DBUG Then Debug.Print s, Format(dt, "yyyy-mm-dd"), v
                Else
                    If DBUG Then Debug.Print "outside range", s, v
                End If
                
            Else
                If DBUG Then Debug.Print "not a date", s, v
            End If
           
        Next
    Next
    ' result
    MsgBox n - 1 & " logs scanned. " & vbLf & _
           r - 2 & " lines extracted", vbInformation
    
End Sub

相关问题