+ Reply to Thread
Results 1 to 15 of 15

Deleting last two characters in a cell

Hybrid View

patentprio Deleting last two characters... 03-02-2010, 06:00 PM
JBeaucaire Re: Deleting last two... 03-02-2010, 06:13 PM
JBeaucaire Re: Deleting last two... 03-02-2010, 06:17 PM
JBeaucaire Re: Deleting last two... 03-02-2010, 06:19 PM
DonkeyOte Re: Deleting last two... 03-03-2010, 03:09 AM
JBeaucaire Re: Deleting last two... 03-03-2010, 03:28 AM
DonkeyOte Re: Deleting last two... 03-03-2010, 03:41 AM
Helpmeexcel_85 Re: Deleting last two... 03-03-2010, 03:51 AM
DonkeyOte Re: Deleting last two... 03-03-2010, 04:03 AM
rwgrietveld Re: Deleting last two... 03-03-2010, 05:09 AM
DonkeyOte Re: Deleting last two... 03-03-2010, 05:23 AM
rwgrietveld Re: Deleting last two... 03-03-2010, 05:55 AM
patentprio Re: Deleting last two... 03-03-2010, 04:15 PM
JBeaucaire Re: Deleting last two... 03-03-2010, 04:37 PM
martindwilson Re: Deleting last two... 03-03-2010, 07:08 PM
  1. #1
    Registered User
    Join Date
    03-02-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2003
    Posts
    6

    Unhappy Deleting last two characters in a cell

    Everyone,

    I would really be grateful if someone could help me with the following:

    In a column I have several rows (on average 100 rows) in which values like the following are defined: EP10000001A1. Thus, it looks something like the following

    EP10000001A1
    EP10000002A1
    EP10000003A1
    EP10000004A1

    I would like to delete the last two characters in ever cell for every row. Hence, taking the above example all "A1" should be deleted so that it should looks like this:

    EP10000001
    EP10000002
    EP10000003
    EP10000004

    As I said, on average I have a 100 rows in a column (sometimes up to 300 rows). Hence, I would really like to avoid doing this manually. How can I achieve this? Is there a standard Macro for this type of operation? (I must admit that I am new to VBA/creation of Macros).

    Thank you in advance.

    Best regards,
    tony
    Last edited by patentprio; 03-04-2010 at 06:11 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Deleting last two characters in a cell

    Try this:
    Sub TruncateColumn()
    Dim LR As Long, LC As Long, vCol As Long
    
    vCol = 1    'column to evaluate... A=1, B=2, etc
    LR = Cells(Rows.Count, vCol).End(xlUp).Row
    
    Application.ScreenUpdating = False
        With Range(Cells(1, Columns.Count), Cells(LR, Columns.Count))
            .FormulaR1C1 = "=LEFT(RC1,LEN(RC1)-2)"
            .Copy
            Cells(1, vCol).PasteSpecial xlPasteValues
            .ClearContents
        End With
    Application.ScreenUpdating = True
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Deleting last two characters in a cell

    DonkeyOte,

    Why doesn't this EVALUATE() construct work?
    Sub TruncateSelection()
    'Select a range and then run the macro to truncate values without looping
    Dim Cell As Range
    
        With Selection.Cells
            .Value = Evaluate("IF(ROW(1:" & Selection.Cells.Count & "),LEFT(" & .Address & ", 12)")
        End With
    
    End Sub

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Deleting last two characters in a cell

    Don, nevermind, I figured out my parens placement problem, again.

    Tony: use this version if you want to select any range and run the macro, it will trim 2 characters off the values regardless of length.

    Sub TruncateSelection()
    'Select a range and then run the macro to truncate values without looping
    Dim Cell As Range
    
        With Selection.Cells
            .Value = Evaluate("IF(ROW(1:" & Selection.Cells.Count & "),LEFT(" & .Address & ", LEN(" & .Address & ")-2))")
        End With
    
    End Sub
    Last edited by JBeaucaire; 03-03-2010 at 04:00 AM.

  5. #5
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Deleting last two characters in a cell

    JB, glad you resolved !

    What I often do if I'm not getting desired results is use Debug.Print on the Evaluate string to see what I'm generating, eg:

    Debug.Print "IF(ROW(1:" & Selection.Cells.Count & "),LEFT(" & .Address & ", 12)"
    I find that generally allows you to identify function errors quite quickly.

    I would still say though that if you opt to work with a Selection you should be sure to iterate by Area to handle possibility of non-contiguous selection at run time.

    On an aside instead of:

    "IF(ROW(1:" & Selection.Cells.Count & ")
    You might find it easier to use:

    "IF(ROW(" & .Address & ")

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Deleting last two characters in a cell

    Quote Originally Posted by DonkeyOte View Post
    You might find it easier to use:
    "IF(ROW(" & .Address & ")
    I get errors on that. When I use your debug suggestion, I get this:

    IF(ROW($A$7:$A$8),LEFT($A$7:$A$8, LEN($A$7:$A$8)-2)

    ...as opposed to this, which works:
    IF(ROW(1:2),LEFT($A$7:$A$8, LEN($A$7:$A$8)-2)

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Deleting last two characters in a cell

    As I see it your last Evaluate is still missing a closing parenthesis, eg:

    .Value = Evaluate("IF(ROW(" & .Address & "),LEFT(" & .Address & ", LEN(" & .Address & ")-2))")

  8. #8
    Registered User
    Join Date
    03-02-2010
    Location
    Chennai,India
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Deleting last two characters in a cell

    Hi Tony,

    You can do this by entering 3 formulas,

    please find the image i attached for your reference.

    hope this will help you.


    Regards,
    Rajesh
    Attached Images Attached Images
    Last edited by Paul; 03-05-2010 at 02:35 AM.

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Deleting last two characters in a cell

    Rajesh, why 3 functions - as already illustrated by JB (via Evaluate) the same can be achieved with one:

    =LEFT(cell,LEN(cell)-2)

    On an aside please do not load your posts with a blog link - put this is in your signature if you want / need to.

  10. #10
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Deleting last two characters in a cell

    DO, that would be in conflict with FR # 13
    13. Cross promotion/Links of any competitor forums to www.excelforum.com in signature of any member are NOT allowed.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Deleting last two characters in a cell

    From what I can tell it's not exactly a competitor...

  12. #12
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Deleting last two characters in a cell

    DO, Must agree

  13. #13
    Registered User
    Join Date
    03-02-2010
    Location
    Switzerland
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Deleting last two characters in a cell

    Everyone, thank you very much. I REALLY appreciate it.

    regards,
    tony

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Deleting last two characters in a cell

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

    (Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated. It is found across from the "time" in each of our posts.)

  15. #15
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Deleting last two characters in a cell

    From what I can tell it's not exactly a competitor...
    must agree it seems every TD&H in certain locations have reinvented the wheel!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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