Solved

Search and Replace VBA

Posted on 2012-03-25
2
244 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

821 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