Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Excel VBA search and replace a string

Posted on 2014-10-29
8
Medium Priority
?
194 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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

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

Technology Partners: 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

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,…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

715 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