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!
 

Some videos you may like

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,209
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)
 

Watch MrExcel Video

Forum statistics

Threads
1,109,001
Messages
5,526,203
Members
409,686
Latest member
Tori83

This Week's Hot Topics

Top