gpt4 book ai didi

VBA使用行输入时如何从下面的行获取文本?

转载 作者:行者123 更新时间:2023-12-03 03:34:13 31 4
gpt4 key购买 nike

我正在尝试将保存为 .txt 文件的电子邮件中的副本信息复制到 Excel 工作表中。我遇到的问题是,当我使用带有行输入函数的 Do 循环时,我无法从不在当前行的文本行获取信息。我还希望代码能够复制包含分隔符的行。

这是我正在使用的代码:

 intFreefile = FreeFile
Open ThisWorkbook.Path & "\temp567.txt" For Input As #intFreefile
lngRecordsInEmail = 0
Do Until EOF(intFreefile)
Line Input #intFreefile, strText
If InStr(1, strText, strDelimiter) > 0 Then
If InStrRev(1, strText, strDelimiter) = 1 Then
' if last character in line = deliminator then
' how do i get the text on 2 lines below?

Else
ws.Cells(lngRow, 1).Value = strText
End If

If blColourCell Then
ws.Cells(lngRow, 1).Interior.ColorIndex = 35
End If
strText2 = strText2 + 1
lngRow = lngRow + 1
lngTotalRecords = lngTotalRecords + 1
lngRecordsInEmail = lngRecordsInEmail + 1
End If
Loop
Close

最佳答案

我最喜欢的方法是一次性将文件中的整个文本读入数组,然后使用该数组,这种方法也比循环遍历文件内容更快。这也将使您能够更好地控制从所需的 2 行中检索文本。

这就是你正在尝试的吗? (未经测试)

Dim MyData As String, strData() As String
Dim i As Long

intFreefile = FreeFile

'~~> Open file and read it on one go
Open ThisWorkbook.Path & "\temp567.txt" For Binary As #intFreefile
MyData = Space$(LOF(1))
Get #intFreefile, , MyData
Close #intFreefile '<~~ Close the text file after reading from it

'~~> This array has the entire contents from the text file
strData() = Split(MyData, vbCrLf)

For i = LBound(strData) To UBound(strData)
'~~> Last character in line = deliminator
If Right(strData(i), 1) = strDelimiter Then
Debug.Print strData(i)
Debug.Print strData(i + 1)
Debug.Print strData(i + 2)
'~~> Else if the deliminator is somewhere else
ElseIf InStr(1, strData(i), strDelimiter) Then
Debug.Print strData(i)
End If
Next i

关于VBA使用行输入时如何从下面的行获取文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44442891/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com