How to search text string within range VBA

Fractalis

Active Member
Joined
Oct 11, 2011
Messages
328
Office Version
  1. 365
Platform
  1. Windows
Hello to all,

I want to search the substring within a range that is not in a single row and if match select the cells where is the string present.


I was able to make a code that works but only if the data is in a single row, but I don't know how to do it if the data is in a range with several rows and columns.


I generate the data with "=DEC2HEX(RANDBETWEEN(0,255),2)" in A1 and then copy across and down in range A1:T10.


For example the data in Range A1:T10 is like this:


Code:
   A     B     C     D     E     F     G     H     I     J     K     L     M     N     O     P     Q     R     S     T 
1  F2    37    C6    7D    DB    00    8E    CB    23    A4    97    B8    20    92    94    69    CC    E7    EF    DA
2  81    10    BC    DA    AD    8A    D3    04    86    35    0E    E9    B7    D5    47    2F    8B    90    B6    3C
3  2B    88    00    A9    D0    [B][COLOR=#ff0000]D1    26    45    43    F1    9E    56    86    0C    8A    E1    7F    E1    26    C5
[/COLOR][/B]4[B][COLOR=#ff0000]  33    3C    9D    3C    39    DD    E5    4C    08    5D    8D    85    2C    96    F3    64    0D    CB    5F    B6
[/COLOR][/B]5[B][COLOR=#ff0000]  79    14    81    DB    89    47    FA    83    84    0D    F5    0E[/COLOR][/B]    D9    5F    19    72    5D    1B    66    EC
6  23    BC    F5    59    D4    43    E3    CC    10    EA    07    C3    63    69    3B    B9    63    2B    52    91
7  61    A7    2E    84    56    E4    F1    D4    86    49    C3    A2    27    60    90    93    80    6D    84    F9
8  1B    93    4D    55    37    A9    8B    27    CF    3F    A9    B8    7C    46    4D    1B    6F    CA    7A    C2
9  2A    67    22    A6    B0    D3    5B    5E    81    AA    04    29    40    19    9B    F0    86    22    27    D7
10 1E    13    E1    3E    37    F6    7E    4E    EB    21    C6    7E    A3    70    3F    59    36    C5    4A    BF
and I want to search within Range A1:T10 the following string


"D1264543F19E56860C8AE17FE126C5333C9D3C39DDE54C085D8D852C96F3640DCB5FB6791481DB8947FA83840DF50E"

The string is present and is shown in red within the range.

Maybe somebody could help me with this problem.

Thanks in advance.
 
Last edited:
Inputbox can handle a length of 254 characters. If you want more than that, you'll have to create a simple form with a textbox in it. But do you really have people typing in 2000 characters? Seems like a huge possibility for typos. Or is it just a copy/paste thing? If so, maybe they can paste to a cell on the sheet (a cell can hold 32767 characters), and then have the macro reference that cell like in my first version.


Edit: Incidentally, you can remove the Debug.Print line from my last version. It was debugging code that's not needed (it doesn't hurt anything though).
 
Last edited:
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Inputbox can handle a length of 254 characters. If you want more than that, you'll have to create a simple form with a textbox in it. But do you really have people typing in 2000 characters? Seems like a huge possibility for typos. Or is it just a copy/paste thing? If so, maybe they can paste to a cell on the sheet (a cell can hold 32767 characters), and then have the macro reference that cell like in my first version.


Edit: Incidentally, you can remove the Debug.Print line from my last version. It was debugging code that's not needed (it doesn't hurt anything though).


Thanks for the information. Then the easiest way is to put the string to search in a particular cell.

Thanks so much for the help.
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,154
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top