I have an input text string in a range (from A1 to AV1), each letter in one cell. The string is
From A1 to AV1 look like this
| A B C D E F G H I J K L M N O P Q R S T U V W X Y Z AA AB AC AD AE AF AG AH AI AJ AK AL AM AN AO AP AQ AR AS AT AU AV
--------------------------------------------------------------------------------------------------------------------------
1 | M i c r o s o f t E x c e l i s a s p r e a d s h e e t d e v e l o p e d b y M i c r o s o f t
I want to be able to search for a substring and if found, select the range where the substring is present.
My current code below it works if the input text string is in the same row, but I'm stuck in how to do if the string is in different rows, for example if the same input text string is in range A1:O4 and I want to search the substring "developed" which begins in N2 and ends in G3.
Sub SelectRangeofSubString()
Rng = Range("A1:AV1")
a = Range("A1").CurrentRegion
aa = WorksheetFunction.Transpose(WorksheetFunction.Transpose(a))
str1 = Join(aa, "")
StringToSearch = "developed"
StringLength = Len(StringToSearch)
Pos = InStr(str1, StringToSearch)
Range(Cells(1, Pos), Cells(1, Pos + StringLength - 1)).Select
End Sub
From A1 to O4 look like this
| A B C D E F G H I J K L M N O
---------------------------------------------------------------
1 | M i c r o s o f t E x c e l i
2 | s a s p r e a d s h e e t d e
3 | v e l o p e d b y M i c r o s
4 | o f t
Thanks for any help
Update
Thanks both. It works in both solutions. My last issue, I tried the same when each cell contains 2 letters, May you help me to select the range in this case too?
For example the stringToSearch = "developed" and data is from range A1:H3
A B C D E F G H
----------------------------------
1 | Mi cr os of tE xc el is
2 | as pr ea ds he et de ve
3 | lo pe db yM ic ro so ft