Excel 在记事本副本中搜索字符串并粘贴到特定条件
我正在搜索宏以在记事本中搜索字符串。它应该搜索一个字符串(“报告”)作为起始字符串,并在(“报告结束”)中结束搜索 错误消息:Excel 在记事本副本中搜索字符串并粘贴到特定条件,excel,vba,automation,Excel,Vba,Automation,我正在搜索宏以在记事本中搜索字符串。它应该搜索一个字符串(“报告”)作为起始字符串,并在(“报告结束”)中结束搜索 错误消息: Err: MsgBox "Error while reading the file.", vbCritical, vbNullString oFS.Close Exit Sub End Sub 在这里,我有一段代码可以搜索开始部分,但它应该会在“报告结束”结束时结束,因为结束字符串复制了我在文本中搜索的全部内容,并将其粘贴到一个新的记事本中。
Err:
MsgBox "Error while reading the file.", vbCritical, vbNullString
oFS.Close
Exit Sub
End Sub
在这里,我有一段代码可以搜索开始部分,但它应该会在“报告结束”结束时结束,因为结束字符串复制了我在文本中搜索的全部内容,并将其粘贴到一个新的记事本中。我已经增强了部分代码,以包括两件事 (1) 查找介于“报告”和“报告结束”之间的内容 (2) 将内容复制到名为xyz.txt的新.txt文件中,该文件保存在我的Z:\驱动器上
For i = LBound(arr) To UBound(arr)
If InStr(1, arr(i), "report", vbTextCompare) Then
'Declare variables for the new output file
Dim sOutputFileNameAndPath As String
Dim FN As Integer
sOutputFileNameAndPath = "Z:\xyz.txt"
FN = FreeFile
'Open new output file
Open sOutputFileNameAndPath For Output As #FN
'While 'end of report' has not been found,
'keep looping to print out contents starting from 'report'
Do While InStr(1, arr(i), "end of report", vbTextCompare) = 0
Debug.Print i + 1, arr(i)
Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = i + 1
Range("B" & Range("B" & Rows.Count).End(xlUp).Row + 1) = arr(i)
'Print into new output file
Print #FN, i + 1 & " " & arr(i)
'increment count
i = i + 1
Loop
'Print out the 'end of report' line as well
Debug.Print i + 1, arr(i)
Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = i + 1
Range("B" & Range("B" & Rows.Count).End(xlUp).Row + 1) = arr(i)
'Print 'end of report' line into new output file as well
Print #FN, i + 1 & " " & arr(i)
'close the new output file
Close #FN
'exit the 'For..Next' structure since 'end of report' has been found
Exit For
End If
Next
您好,cll,如果我必须以不同的起点搜索同一事物,如报告123和报告结尾,我需要像此报告234、报告456、报告678那样搜索,文件夹中有n个文件。你能帮我解决这个问题吗
For i = LBound(arr) To UBound(arr)
If InStr(1, arr(i), "report", vbTextCompare) Then
'Declare variables for the new output file
Dim sOutputFileNameAndPath As String
Dim FN As Integer
sOutputFileNameAndPath = "Z:\xyz.txt"
FN = FreeFile
'Open new output file
Open sOutputFileNameAndPath For Output As #FN
'While 'end of report' has not been found,
'keep looping to print out contents starting from 'report'
Do While InStr(1, arr(i), "end of report", vbTextCompare) = 0
Debug.Print i + 1, arr(i)
Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = i + 1
Range("B" & Range("B" & Rows.Count).End(xlUp).Row + 1) = arr(i)
'Print into new output file
Print #FN, i + 1 & " " & arr(i)
'increment count
i = i + 1
Loop
'Print out the 'end of report' line as well
Debug.Print i + 1, arr(i)
Range("A" & Range("A" & Rows.Count).End(xlUp).Row + 1) = i + 1
Range("B" & Range("B" & Rows.Count).End(xlUp).Row + 1) = arr(i)
'Print 'end of report' line into new output file as well
Print #FN, i + 1 & " " & arr(i)
'close the new output file
Close #FN
'exit the 'For..Next' structure since 'end of report' has been found
Exit For
End If
Next