Results 1 to 4 of 4

Remove Spaces from a Cell using VBA

Threaded View

  1. #1
    Registered User
    Join Date
    06-02-2014
    Posts
    14

    Remove Spaces from a Cell using VBA

    Hi All,

    I am working on a userform that uses a lookup function from one of the text fields. I am using a UK postcode (i.e L27 9HD) and using the LEFT, LEN function to remove the last 4 characters so I can then use the remaining L27 part of the postcode to Lookup the value I need.

    Here is the code I have used and it works fine:
    WorksheetFunction.VLookup(Left(txtPost, Len(txtPost) - 4), Sheets("Postcodes").Range("A1:E5000"), 5, False)
    The problem with this is that it relies on the user inputting the postcode with the 'space' (i.e L27 9HD not L279HD). If the user forgets the space then the code will go into error. I tried the following formula that removes the space between the postcode: =SUBSTITUTE(D1," ","") and then just removed the last 3 characters.

    This works in Excel but I can't seem to get it to work in VBA. I basically want the formula to remove the space if it is there or just proceed if it isn't. My code therefore is:

    WorksheetFunction.VLookup(Left(SUBSTITUTE(txtPost, " ",""), Len(txtPost) - 4), Sheets("Postcodes").Range("A1:E5000"), 5, False)
    The trouble is that the VBA debug doesn't like the SUBSTITUTE formula. Am I missing something in the formula or is there another way around this?

    Thanks
    Last edited by Nulladave; 07-16-2014 at 09:55 AM. Reason: Added Code Tags

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] how to remove blank spaces at the end of cell
    By SEMMatt in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2012, 09:34 PM
  2. Remove Spaces in cell?
    By cjconnor24 in forum Excel General
    Replies: 5
    Last Post: 11-07-2007, 07:50 AM
  3. Remove all spaces in a cell...
    By killertofu in forum Excel Formulas & Functions
    Replies: 8
    Last Post: 02-20-2006, 04:20 PM
  4. Replies: 1
    Last Post: 01-06-2006, 03:23 PM
  5. How to remove spaces in a cell
    By lovebaby in forum Excel General
    Replies: 13
    Last Post: 10-29-2005, 10:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1