VBA search for numbers within a string of text

MonkeyTrainee

New Member
Joined
Aug 14, 2014
Messages
5
My client sends me a spreadsheet every month and I am trying to run a macro where it searches for a number within a cell.
In the below example I have labeled the headers as (Type - Acct #), and then there is data below the headers. Sometimes the client changes the account name. For example, Tax Advantage may be Tax Exempt next month, but the account number (11111) won't change.


Tax Advantage - 11111
US All Cap - 22222Commodities - 33333Global Equity - 44444
-50,00020,00030,000-10,000
-75,00050,000
10,00050,000

<tbody>
</tbody>

I want to run a macro in VBA where it will search for the account number in the headers, and they copy the data below.

Below is my code so far, I am trying to use the InStr function and getting no results. The goal is to select the headers everytime based on the account number, since this won't be changing.

Code:
Sub rebalancing()


Dim c As Range, nr As Long

For Each c In Range("A1:J7")
    If InStr(c.Value, "11111") = 0 Then
    ActiveCell.Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    End If
Next

End Sub

Any help would be appreciated.

Thanks,
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi
Welcome to the board

InStr() returns zero when it does not find the value.

I think you meant:

Code:
If InStr(c.Value, "11111") [SIZE=3][FONT=arial black][B][COLOR=#B22222]>[/COLOR][/B][/FONT][/SIZE] 0 Then
 
Upvote 0

Forum statistics

Threads
1,216,108
Messages
6,128,872
Members
449,475
Latest member
Parik11

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