如何使用 VBA 搜索字符串,然后在找到该字符串时将找到该字符串的整行以及接下来的五行写入 Excel 中的同一单元格?
基本上,我有一个文本文件,我正在使用 VBA 将其导入到 Excel 中。基于字符串,文件中的数据将进入相应列标题下的单元格中。我遇到的问题是我的一些数据在换行符处被切断。由于行输入
的限制。
并非所有值都会发生这种情况,只有像这样带有换行符的值才会发生这种情况:
How To Fix: To remove this functionality, set the following Registry key settings:
Hive: HKEY_LOCAL_MACHINE
Path: System\CurrentControlSet\Services...
Key: SomeKey
Type: DWORD
Value: SomeValue
Related Links: Some URL
我正在尝试将从 How to Fix:...
到 Value:...
的所有内容写入我的 Excel 工作表,在同一个单元格中使用How to fix:...
行上的数据。
我知道Input Line
会自动在换行符处停止并移至下一行。没有我的循环
尝试,这就是下面的代码所做的。
If InStr(inputline, "How to Fix:") <> 0 Then
'Do While Not InStr(inputline, "Related Links:")
' Get rid of special characters in string
For i = 1 To Len(description)
sletter = Mid(description, i, i + 1)
iasc = Asc(sletter)
If Not (iasc <= 153 And iasc >= 32) Then
description = Left(description, i - 1) & " " & Right(description, Len(description) - i)
' End If
'Next
Do Until InStr(inputline, "Related Links:") = 1
description = Replace(description, "How to Fix:", "")
ws.Cells(rowndx4, 7).Value = Trim(description)
Loop
End If
我还尝试使用 FileSystemObject
但它不会向 Excel 工作表打印任何内容。代码如下:
Private Function ScanFile2$(FileToRead2 As String, rowndx4 As Long)
Dim wb As Workbook, ws As Worksheet, i As Long
Dim FNum3 As Integer, inputline As String, whatfile As Integer, testnum As String
Dim description As String
Dim finding As String
Set ws = ActiveWorkbook.Worksheets("Sheet1")
FNum3 = FreeFile()
Dim oFSO As FileSystemObject
Set oFSO = New FileSystemObject
Dim TS As TextStream
Const ForReading = 1
Set TS = oFSO.OpenTextFile(FNum3, ForReading)
Do While TS.AtEndOfStream <> True
inputline = TS.ReadAll
description = inputline
If InStr(inputline, "How To Fix:") <> 0 Then
description = Replace(inputline, "How To Fix:", "")
ws.Cells(rowndx4, 2).Value = inputline
End If
Exit Do
Loop
Close FNum3
Set ws = Nothing
Application.ScreenUpdating = True
ScanFile2 = rowndx4
End Function
最佳答案
这段代码
- 使用
RegExp
删除换行符(替换为|
)以展平字符串 - 然后使用第二个
RegExp
提取每个匹配项
在此处更改文件路径c:\temo\test.txt
底部的示例输入和输出
代码
Sub GetText()
Dim objFSO As Object
Dim objTF As Object
Dim objRegex As Object
Dim objRegMC As Object
Dim objRegM As Object
Dim strIn As String
Dim strOut As String
Dim lngCnt As Long
Set objRegex = CreateObject("vbscript.regexp")
Set objFSO = CreateObject("scripting.filesystemobject")
Set objts = objFSO.OpenTextFile("c:\temp\test.txt")
strIn = objts.readall
With objRegex
.Pattern = "\r\n"
.Global = True
.ignorecase = True
strOut = .Replace(strIn, "|")
.Pattern = "(How to Fix.+?)Related"
Set objRegMC = .Execute(strOut)
For Each objRegM In objRegMC
lngCnt = lngCnt + 1
Cells(lngCnt, 7) = Replace(objRegM.submatches(0), "|", Chr(10))
Next
End With
End Sub
输入
test How To Fix: To remove this functionality, set the following Registry key settings:
Hive: HKEY_LOCAL_MACHINE
Path: System\CurrentControlSet\Services...
Key: SomeKey
Type: DWORD
Value: SomeValue
Related Links: Some URL otherstuff
How To Fix: To remove this functionality, set the following Registry key settings:
Hive: HKEY_LOCAL_MACHINE Path: System\CurrentControlSet\Services...
Key: SomeKey
Type: DWORD
Value: SomeValue2
Related Links: Some URL2
输出
关于excel - 如何使用行输入搜索字符串,然后在 Excel 单元格中打印接下来的 5 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21067749/