我有一個包含以下鏈接的 excel 檔案:

這些鏈接連接到具有以下資料的檔案:

我希望將記事本檔案的黃色部分讀入 .xlsx 檔案的黃色部分(記事本是 .tbl 檔案的打開版本)。每個版本號的虛線部分都不同。(此代碼用于檢查是否使用了正確的貼現曲線)。但是,discount_curve.tbl 格式是下一個程式能夠處理的唯一格式。因此,它在不同的檔案夾中具有相同的名稱。
有沒有辦法 excel/vba 可以每三行讀取一次,而讀取的檔案取決于檔案夾鏈接?我非常喜歡讓整個程序自動化,因為有很多版本號。此外,我不想更改檔案格式,因為我希望該程序盡可能干凈。
有人可以幫我嗎?親切的問候。
uj5u.com熱心網友回復:
似乎您正在尋找常見的 I/O 操作,即逐行讀取檔案。[這里][1]展示了一個很好的例子
為了達到您的目標,我們需要添加一些 if 條件來提取文本檔案的每三行。模除法將是一個好幫手。例如,我們將“i”作為行號,那么我們只需要使 if 條件看起來像這樣:
If (i mod 3) = 0 Then ...
這意味著我們正在尋找每一個除以 3 的“i”給我們余數 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 a file line by line,-Let's read text&text=Open VBA Edit (Alt + F11,and insert the following code.&text=First, a new file is,places it inside a worksheet。
uj5u.com熱心網友回復:
您可以創建一個 User 函式,該函式將從給定檔案中讀取行并回傳第三行。
這是這樣一個功能(免責宣告:此代碼中沒有錯誤管理它可能可以改進很多)
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 定義。
uj5u.com熱心網友回復:
請嘗試下一個函式,如果要提取的必要資料存在于單個檔案中,每三行..它將回傳一個二維陣列,可以在您需要的范圍內一次洗掉:
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
您的圖片未顯示行和列標題。因此,假設您向我們展示的范圍存在于“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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/475612.html
下一篇:轉換陣列Ruby
