Solved

Search and Replace VBA

Posted on 2012-03-25
2
228 Views
Last Modified: 2012-03-25
I need excel vba to search and replace based on certain conditions.


Replace entries in Column D as described below.

ORANGE with ORANGE SODA
GRAPE with GRAPE SODA
RED with RED SODA

then change all the entries that don't match ORANGE SODA, GRAPE SODA, and RED SODA in  column D with SODA POP
0
Comment
Question by:mato01
2 Comments
 
LVL 46

Expert Comment

by:Martin Liss
ID: 37763353
Private Sub CommandButton1_Click()
Dim r As Range
Dim i As Long

Set r = Range("D1").End(xlDown).Offset(0, 0)
For i = 1 To r.Row

    Select Case UCase(Range("D" & i).Value)
        Case "ORANGE"
            Range("D" & i).Value = "ORANGE SODA"
        Case "GRAPE"
            Range("D" & i).Value = "GRAPE SODA"
        Case "RED"
            Range("D" & i).Value = "RED SODA"
        Case Else
            Range("D" & i).Value = "SODA POP"
    End Select
Next

End Sub
0
 
LVL 5

Accepted Solution

by:
chinawal earned 250 total points
ID: 37763419
Columns("D:D").Select
    Selection.Replace What:="ORANGE", Replacement:="ORANGE SODA", LookAt:= _
        xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
   
    Columns("D:D").Select
    Selection.Replace What:="GRAPE", Replacement:="GRAPE SODA", LookAt:= _
        xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False

    Columns("D:D").Select
    Selection.Replace What:="RED", Replacement:="RED SODA", LookAt:= _
        xlWhole, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False


    For Each c In Range("D1:D99999").Cells
        If (c.Value <> "ORANGE SODA" And c.Value <> "GRAPE SODA" And c.Value <> "RED SODA" And c.Value <> "") Then c.Value = "SODA POP"
    Next
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now