我正在尝试导出位于 B 列中的 2 个条目之间的一系列行,这些条目是使用提示手动输入的。例如,提示会询问我第一个和第二个搜索词,我会输入 cat 然后 dog。B5 有单词 cat,B50 有单词 dog。我想捕获第 6 到 49 行,然后将其通过下面的内容并将输出发送到文本文件。
Sub ExportColumnsABToText()
Dim oStream As Object
Dim sTextPath As Variant
Dim sText As String
Dim sText2 As String
Dim sLine As String
Dim sType As String
Dim rIndex As Long, cIndex As Long
sTextPath = Application.GetSaveAsFilename("export.txt", "Text Files, *.txt")
If sTextPath = False Then Exit Sub
sText = ""
For rIndex = 4 To 700
sLine = ""
sType = Sheets![worksheet1].Cells(rIndex, 8).Text
If sType = "A" Or sType = "CNAME" Then
For cIndex = 1 To 2
If cIndex > 1 Then
sLine = sLine & vbTab
End If
sLine = sLine & Sheets![worksheet1].Cells(rIndex, cIndex).Text
Next cIndex
If Not Len(Trim(Replace(sLine, vbTab, ""))) = 0 Then
If rIndex > 4 Then
sText = sText & IIf(sText = "", "", vbNewLine) & sLine
End If
End If
End If
' End If
Next rIndex
Set oStream = CreateObject("ADODB.Stream")
With oStream
.Type = 2
.Charset = "UTF-8"
.Open
.WriteText sText
.SaveToFile sTextPath, 2
.Close
End With
Set oStream = Nothing
End Sub