搜索多个字符串,找到则msgbox,找不到则调用其他sub
Search for multiple strings, msgbox when found, call other sub if not
不得不延长我以前的潜艇。
需要找到三个字符串中的任何一个(错误,但存储为文本)。
如果找到,带有警告的 msgbox 并停止子。
如果没有找到,调用其他sub.
下面是我目前的代码。
问题是,当找到字符串时,我也会调用另一个子程序。
Sub Z_ZWR_sprawdzbledy()
Dim MyAr(1 To 3) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim i As Long
Set ws = ThisWorkbook.Sheets("komunikat_OS_zwroty")
MyAr(1) = "#VALUE!"
MyAr(2) = "#N/A"
MyAr(3) = "#REF!"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = Worksheets("komunikat_OS_zwroty").Cells.Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
MsgBox "UWAGA! Znaleziono bledy!" & vbNewLine & vbNewLine & "SPRAWDZ KOMORKI Z #N/A!, #N/D! lub #REF!"
Else
End If
Next
Call zwrot2
End With
End Sub
从表面上看,无论如何都会调用 zwrot2。
您需要将 Call zwrot2 移动到 For 循环中,然后(我猜)在 ELSE 之后的 IF 语句中。
一个可能的解决方案。代码循环遍历给定范围,如果有错误,将出现消息并将变量“blnCheckErrors”设置为 true。
循环结束后,if 语句将检查“blnCheckErrors”。如果为假,则给定的 procedure/sub 将被执行。
Sub Z_ZWR_sprawdzbledy()
Dim MyAr(1 To 3) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim i As Long
Dim blnCheckErrors as boolean
Set ws = ThisWorkbook.Sheets("komunikat_OS_zwroty")
MyAr(1) = "#VALUE!"
MyAr(2) = "#N/A"
MyAr(3) = "#REF!"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
'我对这里的代码进行了一些优化,因为你确实使用了与上面相同的语句 (ThisWorkbook.Sheets("komunikat_OS_zwroty"))
Set aCell = .Cells.Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell 'Edit: What is this for? It seems that you don't use it again
MsgBox "UWAGA! Znaleziono bledy!" & vbNewLine & vbNewLine & "SPRAWDZ KOMORKI Z #N/A!, #N/D! lub #REF!"
blnCheckErrors = true
End If
Next
'不再需要“调用”语句,只需使用 sub/function
的名称
if blnCheckErrors = false then zwrot2
End With
End Sub
不得不延长我以前的潜艇。 需要找到三个字符串中的任何一个(错误,但存储为文本)。 如果找到,带有警告的 msgbox 并停止子。 如果没有找到,调用其他sub.
下面是我目前的代码。 问题是,当找到字符串时,我也会调用另一个子程序。
Sub Z_ZWR_sprawdzbledy()
Dim MyAr(1 To 3) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim i As Long
Set ws = ThisWorkbook.Sheets("komunikat_OS_zwroty")
MyAr(1) = "#VALUE!"
MyAr(2) = "#N/A"
MyAr(3) = "#REF!"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
Set aCell = Worksheets("komunikat_OS_zwroty").Cells.Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell
MsgBox "UWAGA! Znaleziono bledy!" & vbNewLine & vbNewLine & "SPRAWDZ KOMORKI Z #N/A!, #N/D! lub #REF!"
Else
End If
Next
Call zwrot2
End With
End Sub
从表面上看,无论如何都会调用 zwrot2。 您需要将 Call zwrot2 移动到 For 循环中,然后(我猜)在 ELSE 之后的 IF 语句中。
一个可能的解决方案。代码循环遍历给定范围,如果有错误,将出现消息并将变量“blnCheckErrors”设置为 true。
循环结束后,if 语句将检查“blnCheckErrors”。如果为假,则给定的 procedure/sub 将被执行。
Sub Z_ZWR_sprawdzbledy()
Dim MyAr(1 To 3) As String
Dim ws As Worksheet
Dim aCell As Range, bCell As Range
Dim i As Long
Dim blnCheckErrors as boolean
Set ws = ThisWorkbook.Sheets("komunikat_OS_zwroty")
MyAr(1) = "#VALUE!"
MyAr(2) = "#N/A"
MyAr(3) = "#REF!"
With ws
'~~> Loop through the array
For i = LBound(MyAr) To UBound(MyAr)
'我对这里的代码进行了一些优化,因为你确实使用了与上面相同的语句 (ThisWorkbook.Sheets("komunikat_OS_zwroty"))
Set aCell = .Cells.Find(What:=MyAr(i), LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Not aCell Is Nothing Then
Set bCell = aCell 'Edit: What is this for? It seems that you don't use it again
MsgBox "UWAGA! Znaleziono bledy!" & vbNewLine & vbNewLine & "SPRAWDZ KOMORKI Z #N/A!, #N/D! lub #REF!"
blnCheckErrors = true
End If
Next
'不再需要“调用”语句,只需使用 sub/function
的名称 if blnCheckErrors = false then zwrot2
End With
End Sub