VBA Inserting a certain string error

kdaniel86

New Member
Joined
Aug 1, 2014
Messages
35
Hi all,

I have a simple question about a vba macro i created.

This is a macro where if a certain string "anumber" does not exist in Column A, it will add that string to the end of the column.



Code:
Dim anumber As String

LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                    Set rfind = .Range("A1:A" & LastRow).Find(anumber)
                    If rfind Is Nothing Then
                        If .Range("A" & .Rows.Count).End(xlUp).Offset(-1, 0).Value = "" Then
                            .Range("A" & .Rows.Count).End(xlUp).Offset(-1, 0).Value = anumber
                        Else:
                            .Range("A" & .Rows.Count).End(xlUp).Insert (xlDown)
                            .Range("A" & .Rows.Count).End(xlUp).Offset(-1, 0).Value = anumber
                        End If



The problem, however is that it's failing to add some specific strings. For example,

Let's say my excel column A contains:

Sports15
Sports14
Sports12
Sports25


And I want to add Sports1 to the end of the column. It fails to do so because when you search for Sports1, it actually finds Sports15, and it thinks that Sports1 exists in the column.

How can I make it so that it has to match the exact string?


Any help would be appreciated.

Thanks in advance!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,902
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Change this:

Set rfind = .Range("A1:A" & LastRow).Find(anumber)

to this:

Set rfind = .Range("A1:A" & LastRow).Find(anumber, lookat:=xlWhole)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,017
Messages
5,834,962
Members
430,330
Latest member
drAli77

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
Top