我正在尝试将文本文件转换为Excel工作表。我必须删除一些数据元素并将一些数据元素复制到几列中。要删除一些数据,我必须寻找某个字符串(RUN)。获得该地址后,我必须搜索下一个RUN。在这两个String范围内,我必须搜索另一个String(NET)并将其删除。我必须在整个数据表中这样做,因为这种情况经常发生。
这是我要使用的代码。
Dim name As String: name = "RUN"
Dim secondName As String: secondName = "NET"
Dim rgSearch As Range
' set the range to entire sheet
Set rgSearch = Range(Cells.Address)
Dim rgSearch1 As Range
Dim cell As Range
'search for first occurrence of RUN
Set cell = rgSearch.Find(name)
Dim tempCell As Range
' If not found then exit
If cell Is Nothing Then
Debug.Print "Not found"
Exit Sub
End If
' Store first cell address
Dim firstCellAddress As String, firstRow As Integer, secondRow As Integer
'store address of first result
firstCellAddress = cell.Address
secondRow = cell.Row
Do
'save range to another range for next iteration
Set tempCell = cell.Select
'row variables are for alternate solution I tried
firstRow = secondRow
Debug.Print "Found: " & cell.Address
' search for next instance
Set cell = rgSearch.FindNext(cell)
,set next instance
secondRow = cell.Row
Set rgSearch1 = Range(tempCell, cell).Select
Loop While firstCellAddress <> cell.Address
我也尝试使用
Set rgSearch1 = Range("B" & firstRow + 1 & ":B" & secondRow - 1).Select
而不是将单元格放入范围内,但得到相同的结果。这就是为什么那些firstRow和secondRow变量在那里的原因。
有了这两种想法,我遇到了Object Required错误。有人可以告诉我我在做什么错吗?