Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

replace space with underscore

Posted on 2003-10-26
8
Medium Priority
?
3,343 Views
Last Modified: 2012-06-27
How do I replace space with underscore as such....

"Replacing this string with underscore"  into "Replacing_this_string_with_underscore" in VBA
0
Comment
Question by:BuggyLogic
[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
8 Comments
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 80 total points
ID: 9623496
You can use the replace command:

Private Sub Form_Load()
    Dim a As String, b As String
    a = "Replacing this string with underscore"
    Debug.Print "a = " & a
   
    b = Replace(a, " ", "_")
    Debug.Print "b = " & b
End Sub
0
 
LVL 81

Expert Comment

by:byundt
ID: 9623521
Here is a Function that works in Excel VBA for your purpose:

Function FindAndReplace(strSource As String, strFind As String, strReplace As String)
Dim i As Long, temp As String
temp = strSource
Do
i = InStr(1, temp, strFind)
If i = 0 Then Exit Do
    temp = Left(temp, i - 1) & strReplace & Right(temp, Len(temp) - i - Len(strFind) + 1)
Loop
FindAndReplace = temp
End Function
0
 
LVL 3

Expert Comment

by:gafoorgk
ID: 9623528
there is a built-in function in vba

Function Replace(Expression As String, Find As String, Replace As String, [Start As Long = 1], [Count As Long = -1], [Compare As VbCompareMethod = vbBinaryCompare]) As String
    Member of VBA.Strings
    Find and replace a substring within a string

eg:-
    Replace("Replacing this string with underscore", " ", "_")
0
 
LVL 81

Expert Comment

by:byundt
ID: 9623556
Right you are gafoorgk--and it's a much better solution than my function
0
 
LVL 1

Expert Comment

by:VB-Expert
ID: 9624343
Below is a basic search and replace program

Replacing this string with underscore"  into "Replacing_this_string_with_underscore
Below is a basic search and replace program

Private Sub cmdrepl_Click()
    Dim strTemp As String
    Dim strSrch As String
    Dim strRepl As String

    strTemp = "Replacing this string with underscore"
    strSrch = " "
    strRepl = "_"

   'replace all occurrences of 'strSrch' in 'strTemp' with 'strRepl'
    strTemp = Replace(strTemp, strSrch, strRepl)
End Sub


In this code it is incoporated into a button click routine with hardcoded values in the function
Private Sub cmdrepl_Click()
        txtnewstr.Text = Replace(txtOrigStr, " ", "_")
        txtnewstr.Refresh
End Sub
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

636 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