Solved

replace space with underscore

Posted on 2003-10-26
8
3,064 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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
Vb.net 2008 2 60
Crystal reports - Formula Field code need assistance with code 17 73
MS SQL store procedure to calculate and return result 6 58
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…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

809 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