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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
Change this:

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

to this:

Set rfind = .Range("A1:A" & LastRow).Find(anumber, lookat:=xlWhole)
 
Upvote 0

Forum statistics

Threads
1,217,332
Messages
6,135,945
Members
449,974
Latest member
riffburn

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