二进制访问读取查找特定字符串
Binary access read find specific string
Sub ReadEntireFileAndPlaceOnWorksheet()
Dim X As Long, FileNum As Long, TotalFile As String, FileName As String, Result As Variant, Lines() As String
FileName = "C:\Users\Mohamed samatar.DSSE-EMEA\Documents\EQVL\Test\WHVP113_140910_TTinsug_TT_299Data_PUoff_WOT-TakeOff_NotKickDown_gearD_FelLambda.dat"
FileNum = FreeFile
Open FileName For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Lines = Split(TotalFile, vbNewLine)
ReDim Result(1 To UBound(Lines) + 1, 1 To 1)
For X = 1 To UBound(Result)
Result(X, 1) = Lines(X - 1)
Next
Range("A1").Resize(UBound(Result)) = Result
End Sub
我有一些 .dat 格式的文件,这些文件包含一些有价值的信息,但是它们可能非常大,尝试在记事本中打开每个文件并提取我需要的信息根本没有效率,因为它需要记事本很长一段时间打开每个文件。我遇到过这个 Binary Access Read 函数,它显然可以打开大文件并允许您非常快速地阅读它们。我想知道如何查找/获取特定行的信息,您可以使用类似的功能来说明查找功能,还是有另一种获取信息的方法,这就是我目前所拥有的。它所做的只是告诉我文件类型,基本上我希望能够搜索特定的字符串值,或者如果我可以将所有文本转储到 EXCEL 中并从那里进行排序,任何指导都是有用的。
这里有两个例子。第一个读取文件中的每一行并打印到从单元格 A1 开始的工作表,然后向下到 A2、A3 等,每个 line:
Option Explicit
Sub testReadLine()
Dim filename As String
Dim FF As Integer
Dim line As String
Dim i as Long
filename = "C:\yourfilename.txt" '### MODIFY AS NEEDED
FF = FreeFile
Open filename For Input As FF
Do While Not EOF(FF)
Line Input #FF, line
Range("A1").Offset(i).Value = i
i = i + 1
Loop
Close FF
End Sub
第二个例子将整个文件读入一个字符串变量,然后使用Split
函数将该字符串转换为字符串数组,根据行分隔符(假设为vbCRLF
).
这个应该更快,当然一旦你在 Excel 中有了数据,你就可以使用正常的 excel 功能,如 "Find" 等
Option Explicit
Option Base 0
Sub testReadAll()
Dim filename As String
Dim FF As Integer
Dim text As String
Dim buffer As Long
Dim txtArray() As String
filename = "C:\yourfilename.txt" '### MODIFY AS NEEDED
FF = FreeFile
Open filename For Input As FF
buffer = LOF(FF)
text = Input(buffer, FF)
Close FF
'Put the text into an array, split by the LineFeed character:
txtArray = Split(text, vbCrLf)
'Print to the worksheet:
Range("A1").Resize(Ubound(txtArray)).Value = Application.Transpose(txtArray)
End Sub
Sub ReadEntireFileAndPlaceOnWorksheet()
Dim X As Long, FileNum As Long, TotalFile As String, FileName As String, Result As Variant, Lines() As String
FileName = "C:\Users\Mohamed samatar.DSSE-EMEA\Documents\EQVL\Test\WHVP113_140910_TTinsug_TT_299Data_PUoff_WOT-TakeOff_NotKickDown_gearD_FelLambda.dat"
FileNum = FreeFile
Open FileName For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Lines = Split(TotalFile, vbNewLine)
ReDim Result(1 To UBound(Lines) + 1, 1 To 1)
For X = 1 To UBound(Result)
Result(X, 1) = Lines(X - 1)
Next
Range("A1").Resize(UBound(Result)) = Result
End Sub
我有一些 .dat 格式的文件,这些文件包含一些有价值的信息,但是它们可能非常大,尝试在记事本中打开每个文件并提取我需要的信息根本没有效率,因为它需要记事本很长一段时间打开每个文件。我遇到过这个 Binary Access Read 函数,它显然可以打开大文件并允许您非常快速地阅读它们。我想知道如何查找/获取特定行的信息,您可以使用类似的功能来说明查找功能,还是有另一种获取信息的方法,这就是我目前所拥有的。它所做的只是告诉我文件类型,基本上我希望能够搜索特定的字符串值,或者如果我可以将所有文本转储到 EXCEL 中并从那里进行排序,任何指导都是有用的。
这里有两个例子。第一个读取文件中的每一行并打印到从单元格 A1 开始的工作表,然后向下到 A2、A3 等,每个 line:
Option Explicit
Sub testReadLine()
Dim filename As String
Dim FF As Integer
Dim line As String
Dim i as Long
filename = "C:\yourfilename.txt" '### MODIFY AS NEEDED
FF = FreeFile
Open filename For Input As FF
Do While Not EOF(FF)
Line Input #FF, line
Range("A1").Offset(i).Value = i
i = i + 1
Loop
Close FF
End Sub
第二个例子将整个文件读入一个字符串变量,然后使用Split
函数将该字符串转换为字符串数组,根据行分隔符(假设为vbCRLF
).
这个应该更快,当然一旦你在 Excel 中有了数据,你就可以使用正常的 excel 功能,如 "Find" 等
Option Explicit
Option Base 0
Sub testReadAll()
Dim filename As String
Dim FF As Integer
Dim text As String
Dim buffer As Long
Dim txtArray() As String
filename = "C:\yourfilename.txt" '### MODIFY AS NEEDED
FF = FreeFile
Open filename For Input As FF
buffer = LOF(FF)
text = Input(buffer, FF)
Close FF
'Put the text into an array, split by the LineFeed character:
txtArray = Split(text, vbCrLf)
'Print to the worksheet:
Range("A1").Resize(Ubound(txtArray)).Value = Application.Transpose(txtArray)
End Sub