[英]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.