Solved

Excel VBA search and replace a string

Posted on 2014-10-29
8
171 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 5

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
 
LVL 33

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 25

Assisted Solution

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

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 33

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now