Solved

Excel VBA search and replace a string

Posted on 2014-10-29
8
187 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
[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
  • 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 167 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 34

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 26

Assisted Solution

by:ProfessorJimJam
ProfessorJimJam earned 166 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 34

Accepted Solution

by:
Norie earned 167 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
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…

710 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