Solved

replace space with underscore

Posted on 2003-10-26
8
3,090 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
8 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 20 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: 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

Suggested Solutions

Title # Comments Views Activity
Paint/Redraw window while dragging 16 82
message box in access 4 51
Exit a vb6 apps when a calling it apps closes 15 52
Access Object Property from VBA Module in Excel 2010 2 37
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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…

856 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