Solved

Search and Replace VBA

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

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
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.

724 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