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 - 22222
Commodities - 33333
Global Equity - 44444
-50,000
20,000
30,000
-10,000
-75,000
50,000
10,000
50,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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
how about this:

Code:
Sub acct()
Dim x As Integer, finalcol, accountnum As Long
accountnum = InputBox("Please enter the account number you want to find", "Account Search", , 9500, 5500)
x = 1
finalcol = cells(1, Columns.Count).End(xlToLeft).Column
 Do
    If InStr(cells(1, x).Value, accountnum) Then
       finalrow = cells(Rows.Count, x).End(xlUp).Row
       Range(cells(1, x), cells(finalrow, x)).Copy
       Exit Do
    End If
    x = x + 1
 Loop Until x > finalcol
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,574
Messages
6,131,492
Members
449,653
Latest member
aurelius33

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