我使用下面的代码在工作表中查找文本然后循环查找下一个匹配项,直到它找不到任何新的匹配项。但我得到一个运行时错误13;我第二次运行时输入Mismatch错误。它第一次工作,但在第二次运行代码时生成错误。
类型不匹配错误发生在Set currentFind ... line
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim xlSh As Excel.Worksheet
Dim rcell As Range
Dim int_row As Integer
Dim str_platelet_count_address As String
Dim int_firstFind_count As Integer
Dim currentFind As Excel.Range
Dim d As Excel.Range
Dim str_firstFind_address As String
Dim bln_found As Boolean
Dim str_din As String
Dim int_platelet_count As Integer
Set xlApp = CreateObject("Excel.Application")
xlApp.Visible = True
xlApp.DisplayAlerts = False ' ensures Excel default which is to overwrite files
Set xlWB = xlApp.Workbooks.Open(Me.txt_workday_file, True, False)
Set xlSh = xlWB.ActiveSheet
xlSh.Range("A1").Select
Set currentFind = xlSh.Cells.Find(What:="W2017", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If currentFind Is Nothing Then
MsgBox "Can't find"
GoTo Close_workbook
Else
str_firstFind_address = currentFind.Address
currentFind.Select
End If
Do Until currentFind Is Nothing
int_row = ActiveCell.Row
str_din = ActiveCell.Value
str_platelet_count_address = "J" & int_row - 1
int_platelet_count = Range(str_platelet_count_address).Value
msgbox "Found"
Cells.FindNext(After:=ActiveCell).Activate
If str_firstFind_address = ActiveCell.Address Then
int_firstFind_count = int_firstFind_count + 1
End If
If int_firstFind_count >= 1 Then GoTo Close_workbook
Loop
Close_workbook:
xlWB.Close
xlApp.Quit
Set xlApp = Nothing
Set currentFind = Nothing
Set xlWB = Nothing
Set xlSh = Nothing
DoCmd.SetWarnings False
MsgBox "Done"
End Sub