Solved

Search and Replace VBA

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

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

707 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

17 Experts available now in Live!

Get 1:1 Help Now