在 excel 中提取特定行的 .tbl 文件
extract specific rows of .tbl files in excel
我有一个包含以下 link 的 excel 文件:
这些 link 已连接到具有以下数据的文件:
我想将记事本文件的黄色部分读入.xlsx文件的黄色部分(记事本是.tbl文件的打开版本)。每个版本号的虚线部分不同。 (此代码用作检查是否使用了正确的折扣曲线)。但是,discount_curve.tbl 格式是下一个使用的程序能够处理的唯一格式。因此,它具有相同的名称,只是在不同的文件夹中。
有没有一种方法 excel/vba 可以每隔三行读取一次,同时读取的文件取决于文件夹 link?我非常喜欢让整个过程自动化,因为有很多版本号。此外,我不想更改文件格式,因为我希望过程尽可能干净。
有人能帮帮我吗?
亲切的问候。
您似乎在寻找常见的 I/O 操作,即逐行读取文件。
[这里][1]
展示了很好的例子
为了达到您的目标,我们需要添加一些 if-conditions 来提取文本文件的每三行。
模除法将是一个好帮手。
例如我们有 'i' 作为行号
那么我们只需要使 if 条件看起来像这样:
If (i mod 3) = 0 Then ...
这意味着我们正在寻找每个 'i' 除以 3 的余数为 0
这样我们的代码看起来像这样
Sub ReadFileLineByLine()
Dim my_file As Integer
Dim text_line As String
Dim file_name As String
Dim i As Integer
file_name = "C:\text_file.txt"
my_file = FreeFile()
Open file_name For Input As my_file
i = 1
While Not EOF(my_file)
Line Input #my_file, text_line
If (i mod 3) = 0 Then
Cells(i, "A").Value = text_line
End If
i = i + 1
Wend
结束子
[1]: https://excel.officetuts.net/vba/read-a-text-file/#:~:text=Reading%20a%20file%20line%20by%20line,-Let's%20read%20text&text=打开%20VBA%20Edit%20(Alt%20%2B%20F11,and%20insert%20the%20following%20code.&text=First%2C%20a%20new%20file%20is,places%20it%20inside%20a%20worksheet.
您可以创建一个用户函数来读取给定文件中的行,return 第三个。
这是一个这样的函数(免责声明:此代码中没有错误管理,可能会改进很多)
Function Get3rdLine(filename As String)
Dim f As Long
f = FreeFile
Open filename For Input As f
Line Input #f, Get3rdLine ' just ignore this line
Line Input #f, Get3rdLine ' and this one too
Line Input #f, Get3rdLine ' and return this one
Close #f
End Function
你可以用你要读取的文件的路径来调用它:
=Get3rdLine(CONCATENATE(A1,B1,C1))
例如,如果您的路径由单元格 A1、B1 和 C1 定义。
请尝试下一个功能,如果要提取的必要数据存在于单个文件中,每三行..它将return一个2D数组可以在您需要的范围内一次删除:
Function extractThirdLine(filePath As String) As Variant
Dim arrTxt, i As Long, arrFin, k As Long
'read the file content in an array:
arrTxt = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath, 1).ReadAll, vbCrLf)
ReDim arrFin(1 To Int(UBound(arrTxt) / 3) + 1, 1 To 1)
For i = 2 To UBound(arrTxt) Step 3 'start from 2, because arrTxt is 1D array
k = k + 1
arrFin(k, 1) = arrTxt(i) 'build the filal array containing the necessary rows
Next i
extractThirdLine = arrFin
End Function
您的图片没有显示行和列headers。所以,假设你给我们显示的范围存在于“A:C”列中,而你需要将提取的数据放在“D:D”列中,请使用下一种方式:
Sub testExtractThirdLine()
Dim filePath As String, arrVal, el
filePath = "your text file full name" 'please write here the correct file name
arrVal = extractThirdLine(filePath)
Range("D1").Resize(UBound(arrVal), 1).value = arrVal
End Sub
如果您显示的范围不是我认为的范围,您可以很容易地调整 Range("D1")
紧接在列范围之后的行,它的行是讨论范围的第一行。
如果有什么地方不够清楚,请不要犹豫,要求澄清。
已编辑:
但是如果可以在文件中找到每三行,对于每一行,并且通过连接三列获得相应文件的路径,下一个函数将完成这项工作:
Function extractLine(filePath As String) As String
extractLine = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath, 1).ReadAll, vbCrLf)(2)
End Function
可以这样称呼:
Sub extractStrings()
Dim i As Long, arr, arrFin, lastRow As Long
lastRow = Range("A" & rows.count).End(xlUp).Row 'supposing that 'C:\' exists in A:A column
arr = Range("A2:C" & lastRow).value
ReDim arrFin(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
arrFin(i, 1) = extractLine(arr(i, 1) & arr(i, 2) & arr(i, 3))
Next i
'drop the processed array content at once:
Range("D2").Resize(UBound(arrFin), 1).value = arrFin
End Sub
我有一个包含以下 link 的 excel 文件:
这些 link 已连接到具有以下数据的文件:
我想将记事本文件的黄色部分读入.xlsx文件的黄色部分(记事本是.tbl文件的打开版本)。每个版本号的虚线部分不同。 (此代码用作检查是否使用了正确的折扣曲线)。但是,discount_curve.tbl 格式是下一个使用的程序能够处理的唯一格式。因此,它具有相同的名称,只是在不同的文件夹中。
有没有一种方法 excel/vba 可以每隔三行读取一次,同时读取的文件取决于文件夹 link?我非常喜欢让整个过程自动化,因为有很多版本号。此外,我不想更改文件格式,因为我希望过程尽可能干净。
有人能帮帮我吗? 亲切的问候。
您似乎在寻找常见的 I/O 操作,即逐行读取文件。 [这里][1]
展示了很好的例子为了达到您的目标,我们需要添加一些 if-conditions 来提取文本文件的每三行。 模除法将是一个好帮手。 例如我们有 'i' 作为行号 那么我们只需要使 if 条件看起来像这样:
If (i mod 3) = 0 Then ...
这意味着我们正在寻找每个 'i' 除以 3 的余数为 0 这样我们的代码看起来像这样
Sub ReadFileLineByLine()
Dim my_file As Integer
Dim text_line As String
Dim file_name As String
Dim i As Integer
file_name = "C:\text_file.txt"
my_file = FreeFile()
Open file_name For Input As my_file
i = 1
While Not EOF(my_file)
Line Input #my_file, text_line
If (i mod 3) = 0 Then
Cells(i, "A").Value = text_line
End If
i = i + 1
Wend
结束子
[1]: https://excel.officetuts.net/vba/read-a-text-file/#:~:text=Reading%20a%20file%20line%20by%20line,-Let's%20read%20text&text=打开%20VBA%20Edit%20(Alt%20%2B%20F11,and%20insert%20the%20following%20code.&text=First%2C%20a%20new%20file%20is,places%20it%20inside%20a%20worksheet.
您可以创建一个用户函数来读取给定文件中的行,return 第三个。
这是一个这样的函数(免责声明:此代码中没有错误管理,可能会改进很多)
Function Get3rdLine(filename As String)
Dim f As Long
f = FreeFile
Open filename For Input As f
Line Input #f, Get3rdLine ' just ignore this line
Line Input #f, Get3rdLine ' and this one too
Line Input #f, Get3rdLine ' and return this one
Close #f
End Function
你可以用你要读取的文件的路径来调用它:
=Get3rdLine(CONCATENATE(A1,B1,C1))
例如,如果您的路径由单元格 A1、B1 和 C1 定义。
请尝试下一个功能,如果要提取的必要数据存在于单个文件中,每三行..它将return一个2D数组可以在您需要的范围内一次删除:
Function extractThirdLine(filePath As String) As Variant
Dim arrTxt, i As Long, arrFin, k As Long
'read the file content in an array:
arrTxt = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath, 1).ReadAll, vbCrLf)
ReDim arrFin(1 To Int(UBound(arrTxt) / 3) + 1, 1 To 1)
For i = 2 To UBound(arrTxt) Step 3 'start from 2, because arrTxt is 1D array
k = k + 1
arrFin(k, 1) = arrTxt(i) 'build the filal array containing the necessary rows
Next i
extractThirdLine = arrFin
End Function
您的图片没有显示行和列headers。所以,假设你给我们显示的范围存在于“A:C”列中,而你需要将提取的数据放在“D:D”列中,请使用下一种方式:
Sub testExtractThirdLine()
Dim filePath As String, arrVal, el
filePath = "your text file full name" 'please write here the correct file name
arrVal = extractThirdLine(filePath)
Range("D1").Resize(UBound(arrVal), 1).value = arrVal
End Sub
如果您显示的范围不是我认为的范围,您可以很容易地调整 Range("D1")
紧接在列范围之后的行,它的行是讨论范围的第一行。
如果有什么地方不够清楚,请不要犹豫,要求澄清。
已编辑:
但是如果可以在文件中找到每三行,对于每一行,并且通过连接三列获得相应文件的路径,下一个函数将完成这项工作:
Function extractLine(filePath As String) As String
extractLine = Split(CreateObject("Scripting.FileSystemObject").OpenTextFile(filePath, 1).ReadAll, vbCrLf)(2)
End Function
可以这样称呼:
Sub extractStrings()
Dim i As Long, arr, arrFin, lastRow As Long
lastRow = Range("A" & rows.count).End(xlUp).Row 'supposing that 'C:\' exists in A:A column
arr = Range("A2:C" & lastRow).value
ReDim arrFin(1 To UBound(arr), 1 To 1)
For i = 1 To UBound(arr)
arrFin(i, 1) = extractLine(arr(i, 1) & arr(i, 2) & arr(i, 3))
Next i
'drop the processed array content at once:
Range("D2").Resize(UBound(arrFin), 1).value = arrFin
End Sub