+ Reply to Thread
Results 1 to 11 of 11

Code to delete a single character from a single cell

  1. #1
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Code to delete a single character from a single cell

    Hi,

    As a final touch for a macro (which I already have), I'd like to delete a single character from cell A2, and leave a blank space, but the code I'm using does nothing.

    The content of the cell might have one or 2 instances of a dash (-), like this: ABC-123 or AB-C-123. I need it like this: ABC 123 or AB C 123

    This is the line I have: Range("A2").Characters(45).Delete

    What am I missing? Is there a better approach to this?

    Thanks for any help!

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code to delete a single character from a single cell

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: Code to delete a single character from a single cell

    Hi Cytop,

    Thank you so much for your help. I tried the code, however, Excel gives me a Compile error message as follows: "Wrong number of arguments or invalid property assignment", and it highlights the word "Replace". For testing purposes, I'm running the code on its own (no other code involved). Not sure what's going on?

  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code to delete a single character from a single cell

    As far as I can see, on a phone, the syntax is correct. About the only possible issue is if the cell is blank but I can't test at the moment.

    Have a read of https://blog.udemy.com/excel-vba-replace/

  5. #5
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Code to delete a single character from a single cell

    cytop's code works for me. Maybe try:

    Please Login or Register  to view this content.
    This should work also:

    Please Login or Register  to view this content.
    Note: Chr(45) instead of Characters(45) in post #1. Although your line would create an error anyway.

  6. #6
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code to delete a single character from a single cell

    Second thoughts on the compiler error. The code broke over 2 lines (Wasn't me, Guv. Honest)

    If you copied it literally then the compiler will complain. Make all on 1 line.

  7. #7
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: Code to delete a single character from a single cell

    Hi all,

    Well, I guess this is one of those cases where VBA has made tiny changes somewhere resulting in non-working code. These are the results of my tests.

    I kept trying cytop code using Excel 2016 with no success. I changed the target cell and even re-started Excel and ultimately my computer. Nothing. I later thought trying it with Excel 2010. Much to my surprise, it worked with no issues.

    John's code (both options) worked on both versions of Excel.

    I'm not exactly knowledgeable when it comes to VBA, but I'm certainly surprised about this issue.

    Thanks cytop and John for your help. Truly appreciate it!

  8. #8
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: Code to delete a single character from a single cell

    Forgot to mention: I'll try all codes with Excel 2013 tomorrow at work, and I'll add comments about this issue then.

  9. #9
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Code to delete a single character from a single cell

    You're welcome. Glad to help out and thanks for the feedback.

  10. #10
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: Code to delete a single character from a single cell

    Works for me on 2016/2013/2010/2007 and even 2003 & 2002 (Office XP)... and there's no issue if the cell is blank.

  11. #11
    Registered User
    Join Date
    04-20-2013
    Location
    Los Angeles
    MS-Off Ver
    Excel 2010
    Posts
    96

    Re: Code to delete a single character from a single cell

    Hi,

    Well, I tried using Excel 2013 and it worked, but 2016 still gives me an error message. Not sure what's going on here. In all instances, I copied and pasted the code directly into the editor, but only 2016 results in an error.

    In any case, I'm marking this thread as solved, since I got a working code for my need.

    Thank you Cytop and John for your help!

+ Reply to Thread

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 subtract single cells with double digits into a single digit of a single cell.
    By greenfox74 in forum Excel Formulas & Functions
    Replies: 11
    Last Post: 03-11-2021, 02:05 AM
  2. Countif cell contains only a single character
    By Daxxa in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-30-2015, 07:12 AM
  3. Find and delete single character
    By qlpapp in forum Excel General
    Replies: 4
    Last Post: 01-23-2014, 06:03 PM
  4. Find and delete single character
    By KH_GS in forum Excel General
    Replies: 13
    Last Post: 01-23-2014, 04:26 AM
  5. [SOLVED] Need help Converting Code to Read Alpha Numeric Legends instead of single character legend
    By donnydorko in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-28-2012, 10:57 PM
  6. Remove a single character from a cell
    By netman in forum Excel General
    Replies: 2
    Last Post: 01-21-2008, 01:18 PM
  7. Case sensitive and single character in cell
    By praveen_khm in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-07-2006, 10:21 AM

Tags for this Thread

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