Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Excel VBA search and replace a string

Posted on 2014-10-29
8
Medium Priority
?
195 Views
Last Modified: 2014-10-29
I need a code to search dashes - into a string and if found then replace it with space

how I can do that?
0
Comment
Question by:Flora
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40411900
Use this formula

=+SUBSTITUTE(A1;"-";" ")

Where A1 iscell with your data
0
 
LVL 6

Author Comment

by:Flora
ID: 40411910
I know Subs  formula.  my question is on how to use the vba built in function "Replace"  in VBA not in Built in function of Excel
0
 
LVL 15

Assisted Solution

by:Haris Djulic
Haris Djulic earned 668 total points
ID: 40411925
Here is the VBA syntax

Worksheets("SHEET_NAME").Columns("A").Replace What:="-", Replacement:=" ", SearchOrder:=xlByColumns, MatchCase:=True

Open in new window

0
Industry Leaders: 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!

 
LVL 35

Expert Comment

by:Norie
ID: 40411928
This is how you would use replace.

Dim strAString As String

    strAString  ="A-test-string"

    MsgBox Replace(strAString, "-"," ")

Open in new window

0
 
LVL 27

Assisted Solution

by:ProfessorJimJam
ProfessorJimJam earned 664 total points
ID: 40411929
Replace(yourstring, "-", " ")
0
 
LVL 6

Author Comment

by:Flora
ID: 40411942
thank you guys

last query on this,   if I want to replace dashes and underscores - & _ with space
how can this be done?  does it require two times replace or ampersand can be used in replace?
0
 
LVL 35

Accepted Solution

by:
Norie earned 668 total points
ID: 40411957
You'll need to use Replace twice.
Dim strAString As String

    strAString  ="A-test-_string__"

    MsgBox Replace(Replace(strAString, "-"," "), "_", " ")

Open in new window


If you had  lot of different characters you want find/replace you could use an array.
Dim strAString As String
Dim arrChars()
Dim I As Long

    arrChars = Array("-", "_", "?")
    strAString = "A-test-_string__"
    For I = LBound(arrChars) To UBound(arrChars)
        strAString = Replace(strAString, arrChars(I), " ")
    Next I
    
    MsgBox strAString

Open in new window

0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40411958
two times replace..
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

782 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