Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6966717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:13:40+00:00 2026-05-27T16:13:40+00:00

I have this piece of code that originally uses the replace function(VBA) to replace

  • 0

I have this piece of code that originally uses the replace function(VBA) to replace certain characters in a string or column field in MS Access. The code is in a module.

I need to modify the code so that it trims the space around certain characters. Example, if th appears alone, trim the space from the left, so that it flushes against the numbers. If it has more than one tab(space) to the right, trim it so that there is only one tab space to the right. Same for avenue, trim it so that one tab space is on the left and right.

Here is what I have written:

Public Function TrmSpace(RemoveSpace As String) As String
Dim UserEntry As Variant, CorrectedEntry As Variant
Dim i As Long
ExpectedEntry = Array("th ", " th", " th ", "TH")

CorrectedEntry = Array("th", "th", "th", "th")
TrmSpace = RemoveSpace
For i = 0 To 3
    TrmSpace = Trim(TrmSpace, ExpectedEntry(i), CorrectedEntry(i), Compare:=vbTextCompare)
Next
End Function

I changed the function from Replace to Trim but I am doing it wrong.

Thanks everyone!

Guy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:13:40+00:00Added an answer on May 27, 2026 at 4:13 pm

    Trim only removes leading and trailing spaces from the beginning and end of your string. A simple solutions would be:

    Public Function TrmSpace(RemoveSpace As String) As String
    
    RemoveSpace = Replace(RemoveSpace, "    ", " ")
    RemoveSpace = Replace(RemoveSpace, "   ", " ")
    RemoveSpace = Replace(RemoveSpace, "  ", " ")
    RemoveSpace = Replace(RemoveSpace, " th", "th")
    RemoveSpace = Replace(RemoveSpace, " TH", "th")
    
    TrmSpace = RemoveSpace
    End Function
    

    That would remove gaps up to eight spaces. If your data has actual tab characters and not spaces you’d need to replace ” ” with vbTab & vbTab & vbTab & vbTab.


    A single line of code to ‘squeeze’ any number of repeating spaces to a one space and another to remove the leading space for th regardless of case, thus reducing the above to this:

    Public Function TrmSpace(RemoveSpace As String) As String
      TrmSpace = Replace$(Replace$(Replace$(RemoveSpace, Chr$(32), Chr$(32) & Chr$(22)), _
                       Chr$(22) & Chr$(32), vbNullString), Chr$(22), vbNullString)
    
      TrmSpace = Replace$(TrmSpace, " th", "th", , , vbTextCompare)
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code that does not work: public CartaoCidadao() { InitializeComponent();
I have this piece of code that works fine in subsonic 2.2, I migrated
I have this piece of code that is giving me trouble. I know all
Assume that I have this piece of code: @interface Foo : NSObject { Bar
I have this program that should execute a piece of code base on the
I have a piece of code that look similar to this: <xsl:choose> <xsl:when test=some_test>
I have got a piece of code, that should countdown some number (in this
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have this piece of code: $(#faq).click(function () { var url = $.get(faq, {
I have a piece of C code that looks like this: const char (*foo)[2]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.