简体   繁体   English

对于每个单独的文件名,如何从多个文本文件中提取数据?

[英]How do I extract data from multiple text files, in respect to each individual filename?

I need one value from multiple text files.我需要来自多个文本文件的一个值。 Those text files are stored with a 5-digit filename in a folder(Around 1000 files) and I would like to create a macro, which scans this folder for a subset of files and then extract an individual Euro value.这些文本文件以 5 位数的文件名存储在一个文件夹中(大约 1000 个文件),我想创建一个宏,它会扫描该文件夹中的文件子集,然后提取一个单独的欧元值。

屏幕

I got the extraction part going, but I'm not able to loop this process through different file names yet as I'm fairly new to VBA.我得到了提取部分,但我还不能通过不同的文件名循环这个过程,因为我对 VBA 还很陌生。

Sub ExtractData()
    Dim myFile As String, text As String, textline As String, Data As Integer, filename As String
    Dim myFolder As String

    myFolder = "C:\Folder\"
    filename = Range("A1").Value & ".txt"
    myFile = "C:\Folder\" & filename & ""

    Open myFile For Input As #1
    Do Until EOF(1)
        Line Input #1, textline
        text = text & textline
    Loop
    Close #1

    Data = InStr(text, "Euro")
    Range("B1").Value = Mid(text, Data + 6, 4)

End Sub

I would highly appreciate it if someone would point me in the right direction.如果有人能指出我正确的方向,我将不胜感激。 Greetings问候

You may use Scripting.FileSystemObject to iterate the files in the target folder, use the Like operator to validate the file name and then get the value from each file as usual.您可以使用Scripting.FileSystemObject来迭代目标文件夹中的文件,使用Like运算符来验证文件名,然后像往常一样从每个文件中获取值。

This should work:这应该有效:

Sub ExtractData()
    Dim folderPath As String, filePath As String
    Dim textline As String, data As Integer
    folderPath = "C:\Folder\"

    Dim oFso As Object: Set oFso = CreateObject("Scripting.FileSystemObject")
    Dim oFolder As Object: Set oFolder = oFso.GetFolder(folderPath)
    Dim oFiles As Object: Set oFiles = oFolder.Files
    Dim oFile As Object

    Dim counter As Integer
    For Each oFile In oFiles
        If Not oFile.Name Like "#####.txt" Then GoTo ContinueFor

        data = 0
        counter = counter + 1
        Range("A" & counter).Value = oFile.Name
        filePath = folderPath & oFile.Name

        Open filePath For Input As #1
            Do Until EOF(1) Or data > 0
                Line Input #1, textline
                data = InStr(textline, "Euro")
            Loop
        Close #1

        If data > 0 Then Range("B" & counter).Value = Mid(textline, data + 6, 4)
ContinueFor:
    Next
End Sub

This will extract the target value from the first line that contains the word "Euro".这将从包含单词“Euro”的第一行中提取目标值。 If the value that you're trying to extract is not in the same line, you can read the whole text (similar to what you did originally) and then extract the value you want:如果您尝试提取的值不在同一行中,您可以阅读整个文本(类似于您最初所做的),然后提取您想要的值:

Dim allText As String
' ...
' ...

Open filePath For Input As #1
    allText = Input(LOF(1), 1)
Close #1

data = InStr(allText, "Euro")
If data > 0 Then Range("B" & counter).Value = Mid(allText, data + 6, 4)

There are probably better ways but it all depends on the structure of your file (which you haven't shown).可能有更好的方法,但这完全取决于文件的结构(您没有显示)。 For example, if the target value is in the next line and you know its position in that line, you could use the original code above to read the line that contains the word "Euro", read the next line, and then extract the value.例如,如果目标值在下一行,并且您知道该行中的 position,则可以使用上面的原始代码读取包含单词“Euro”的行,读取下一行,然后提取值.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 从多个文本文件中提取数据到 Excel? (每张纸一个文件的数据) - How do I extract data from multiple text files to Excel using Python? (One file's data per sheet) 如何从Excel文件夹中的多个文件中提取数据 - How can I extract data from multiple files in a folder of excel 如何将多个工作表中的数据提取到动态列表中? - How do I extract data from multiple worksheets into a dynamic list? 从文件夹中的多个文本文件中提取数据到excel工作表中 - extract data from multiple text files in a folder into excel worksheet 从众多文本文件中提取多行数据,并关联到Excel中 - Extract multiple lines of data from numerous text files and correlate into Excel 用于从多个文本文件中提取数据到 Excel 电子表格的 Powershell 脚本 - Powershell script to extract data from multiple text files into an excel spreadsheet 将每个工作表保存为来自多个工作簿的单独 pdf 文件 - Saving each worksheet as individual pdf files from multiple workbooks 如何从文件夹中的多个文件中读取数据并将其提取到 csv/xls 中? - How to read and extract data from multiple files in a folder into csv/xls? 如何读取多个文本文件并在某些条件下提取数据,然后基于python中的另一个查找表添加列 - How can I read multiple text files and extract data with some conditions, then add columns based on another lookup table in python 如何从多个文件中提取相同的 excel 表? - How can I extract the same excel sheet from multiple files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM