[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Search and Replace VBA

Posted on 2012-03-25
2
Medium Priority
?
278 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 49

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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

649 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