How can I add a specific text to the end of a column using VBA?

kdaniel86

New Member
Joined
Aug 1, 2014
Messages
35
For example,

An excel file with

A1
=======
Apple
Banana
Kiwi
Watermelon


I need the vba script to loop through the A1 column, and if a specific text does not exist, it will add the text to the very bottom of the column.

so for example, if i wanted to add peach, the result would be

A1
=======
Apple
Banana
Kiwi
Watermelon
Peach

since peach does not exist in A1. Also, if let's say I wanted to add "apple" but "apple" already exists, I would like to add text to the column beside it saying "already exists."

A1 B1
=======
Apple Already exists.
Banana
Kiwi
Watermelon



how can i do this using vba?

Any help would be appreciated.

Thanks.
 
I think I understand now. Please see if this is what you want to do:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rFind As Range
If Target.Address = "$C$1" Then
    Application.EnableEvents = False
    Set rFind = Range("A1:A" & Range("A" & Rows.Count).End(xlUp).Row).Find(Target.Value)
    If rFind Is Nothing Then
        Range("A" & Rows.Count).End(xlUp).Offset(1, 0).Value = Target.Value
    Else
        Range("B:B").ClearContents
        rFind.Offset(0, 1).Value = "Already exists"
    End If
    Target.Select
    Application.EnableEvents = True
End If
End Sub
 
Upvote 0

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Forum statistics

Threads
1,216,069
Messages
6,128,602
Members
449,460
Latest member
jgharbawi

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