I am trying to select a range of cells between two terms. The first term is clearly set, the last one must be nearest, the first out of three possible terms. I tried to adapt the solution from here:
Using an Or function within Range.Find VBA
My range starts with a certain term. But it may end with at least one of three different terms. I would like to select a range between my searched word and the closest of the three ending terms.
So far my code looks as follows.
Sub FindData(sheetName, termA)
Dim foundA As Range, _
foundB As Range, _
foundC As Range, _
foundD As Range, _
foundEnd As Range
With Sheets(sheetName).Columns(1)
Set foundA = .Find(termA)
If Not foundA Is Nothing Then
' my searched end names
Set foundB = .Find("End of table", After:=foundA, `SearchDirection:=xlNext)
Set foundC = .Find("Version", After:=foundA, `SearchDirection:=xlNext)
Set foundD = .Find("Next table", After:=foundA, `SearchDirection:=xlNext)
End If
If Not foundB Is Nothing And Not foundC And Not foundD Is Nothing Then
Set foundEnd = ??????????????????????????????
'The original solution from the link above is not applicable here
'and displays errors, ' ie.
'Set foundEnd = Range("A1").Cells(Application.Min(foundA.Row, foundB.Row, foundC.Row))
End If
Set foundA = foundA.Offset(3, 0)
Set foundEnd = foundEnd.Offset(-1, 3)
Range(foundA, foundEnd).Copy
EndSub
My question, how to set the end of range (the code area with question marks?)
I am a beginner and maybe the solution is simple, I tried help files and other portals. but I could not find a good solution. I hope my question will be useful for the community.
Please let me know if more is required to this question. Thank you for all your help.