+ Reply to Thread
Results 1 to 6 of 6

Strip a letter from the end of a numeric field

  1. #1
    Registered User
    Join Date
    04-29-2013
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    3

    Angry Strip a letter from the end of a numeric field

    When I wrote simple macros using Lotus 1-2-3 (fifteen years ago) I could easily perform simple repetitive activities. For example I have a list of several thousand rows with amounts that have a letter or symbol appended to the end. In lotus I could {edit}{Backspace}~{Down} and each cell would have the letter removed and the value entered in the active cell. When I try this with Visual Basic in Excel, the macro enters the value that is in the cell used to write the macro, i.e., when executed it enters the same value in every cell regardless of what the actual contents are. For example, using the following ten cell contents in Excel
    00000072K
    00000755}
    00000141O
    00000802N
    00000921M
    00001520}
    00000110L
    00001981}
    00001460N
    00000235Q

    The resulting macro for the first five looks like this:
    Sub Macro1()
    '
    ' Macro1 Macro
    ' strip
    '
    ' Keyboard Shortcut: Ctrl+q
    '
    ActiveCell.FormulaR1C1 = "0.72"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "7.55"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "1.41"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "8.02"
    ActiveCell.Offset(1, 0).Range("A1").Select
    ActiveCell.FormulaR1C1 = "9.21"
    ActiveCell.Offset(1, 0).Range("A1").Select
    End Sub

    When I run this macro twice, i.e., once for each 5 cells the results (the same results twice!!) are in the left column… The desired results are in the right column.
    Should be
    0.72 0.72
    7.55 7.55
    1.41 1.41
    8.02 8.02
    9.21 9.21
    0.72 15.2
    7.55 1.1
    1.41 19.81
    8.02 14.6
    9.21 2.35

    What am I doing wrong? How do I get the results I want?

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Strip a letter from the end of a numeric field

    Please Login or Register  to view this content.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Forum Expert
    Join Date
    03-23-2004
    Location
    London, England
    MS-Off Ver
    Excel 2019
    Posts
    7,064

    Re: Strip a letter from the end of a numeric field

    Cant you just use a Helper column, all your data is 9 characters in length
    B1=LEFT(A1,8)*1
    should strip the last character and convert it to numeric
    Regards
    Special-K

    Ensure you describe your problem clearly, I have little time available to solve these problems and do not appreciate numerous changes to them.

  4. #4
    Registered User
    Join Date
    04-29-2013
    Location
    New York, NY
    MS-Off Ver
    Excel 2010
    Posts
    3

    Re: Strip a letter from the end of a numeric field

    Thank you for the suggestion.
    I think I left out one relevant fact that I probably should have mentioned: I created this using the macro recorder. When I wrote macros in Lotus I was able to teach myself as the language was quasi-English. I have tried to learn Visual Basic several times but with almost no success. The macro recorder works nicely only if you are performing EXACTLY the same task as recorded.
    The result you suggested works very nicely but only one cell at a time. Is there a simple way to get this to go down and repeat for the entire length of my column and then stop?

  5. #5
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Strip a letter from the end of a numeric field

    TMShucks' code will work on every cell in the selected range so if you select all your cells first it will adjust all of them ;-)
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,461

    Re: Strip a letter from the end of a numeric field

    @JP: Thanks

    @OlivePetunia: Interestingly, I don't get the Offset.Select statements that you get, I get specific cell selections. Clearly, as well as removing the last character, you add a decimal point.

    Some variations on a theme:

    Please Login or Register  to view this content.

    Typically, recording a macro will repeat exactly what you did when you recorded it and, more often than not, act on the cells you originally changed. To make it more general, you need to tweak it.

    I'm sure that even with Lotus 1-2-3 you would have needed to take the basic recorded code and make it loop around to go through all the cells. And editing a value is pretty much the same as typing it so you would, I believe, have needed some action other than entering an amended value.

    Anyway, the forum is a great place to learn.

    Regards, TMS

+ 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