Solved

replace space with underscore

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

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 80

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

760 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

16 Experts available now in Live!

Get 1:1 Help Now