Хотя это и не прямой ответ на ваш вопрос как таковой, в этом случае вам, вероятно, было бы лучше использовать универсальную функцию FindAll, а затем обрабатывать отдельные ячейки в пределах найденного диапазона, чтобы получить ваши значения.
Я использовал несколько ярлыков, чтобы проиллюстрировать процесс, но вы должны иметь возможность адаптировать вызов FindAll к вашим потребностям.
Sub ProcessDesc()
Dim wsSrc As Worksheet: Set wsSrc = ThisWorkbook.Sheets("Sheet1")
Dim FoundRange As Range, Cell As Range, Val As Integer
Dim DescMaxNumber As Integer: DescMaxNumber = 0
Dim TermDescMaxNumber As Integer: TermDescMaxNumber = 0
Set FoundRange = FindAll("*DESC*", wsSrc.UsedRange)
For Each Cell In FoundRange
Val = IIf(IsNumeric(Right(Cell, 2)), CInt(Right(Cell, 2)), 0)
If InStr(Cell, "TERMDESC") Then
TermDescMaxNumber = IIf(Val > TermDescMaxNumber, Val, TermDescMaxNumber)
Else
DescMaxNumber = IIf(Val > DescMaxNumber, Val, DescMaxNumber)
End If
Next Cell
Debug.Print DescMaxNumber, TermDescMaxNumber
End Sub
Private Function FindAll(What, _
Optional SearchWhat As Variant, _
Optional LookIn, _
Optional LookAt, _
Optional SearchOrder, _
Optional SearchDirection As XlSearchDirection = xlNext, _
Optional MatchCase As Boolean = False, _
Optional MatchByte, _
Optional SearchFormat) As Range
'LookIn can be xlValues or xlFormulas, _
LookAt can be xlWhole or xlPart, _
SearchOrder can be xlByRows or xlByColumns, _
SearchDirection can be xlNext, xlPrevious, _
MatchCase, MatchByte, and SearchFormat can be True or False. _
Before using SearchFormat = True, specify the appropriate settings for the Application.FindFormat _
object; e.g. Application.FindFormat.NumberFormat = "General;-General;""-"""
Dim SrcRange As Range
If IsMissing(SearchWhat) Then
Set SrcRange = ActiveSheet.UsedRange
ElseIf TypeOf SearchWhat Is Range Then
Set SrcRange = IIf(SearchWhat.Cells.Count = 1, SearchWhat.Parent.UsedRange, SearchWhat)
ElseIf TypeOf SearchWhat Is Worksheet Then
Set SrcRange = SearchWhat.UsedRange
Else: SrcRange = ActiveSheet.UsedRange
End If
If SrcRange Is Nothing Then Exit Function
'get the first matching cell in the range first
With SrcRange.Areas(SrcRange.Areas.Count)
Dim FirstCell As Range: Set FirstCell = .Cells(.Cells.Count)
End With
Dim CurrRange As Range: Set CurrRange = SrcRange.Find(What:=What, After:=FirstCell, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If Not CurrRange Is Nothing Then
Set FindAll = CurrRange
Do
Set CurrRange = SrcRange.Find(What:=What, After:=CurrRange, LookIn:=LookIn, LookAt:=LookAt, _
SearchDirection:=SearchDirection, MatchCase:=MatchCase, MatchByte:=MatchByte, SearchFormat:=SearchFormat)
If CurrRange Is Nothing Then Exit Do
If Application.Intersect(FindAll, CurrRange) Is Nothing Then
Set FindAll = Application.Union(FindAll, CurrRange)
Else: Exit Do
End If
Loop
End If
End Function