+ Reply to Thread
Results 1 to 20 of 20

Replace a number with text if it lies within a range/group

  1. #1
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    Replace a number with text if it lies within a range/group

    Hi, I'm trying to build a macro that will replace a number in a cell with a letter if it falls within a specific range. For example.

    I have a list of numbers in Column A which will fall into the following ranges:

    1 - 9 = A
    10 -19 = B
    20 -29 = C

    And if they fall within that range the number would be replaced with a letter.

    Any ideas how I might achieve this?

  2. #2
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    Hello BPSJACK,

    Your question is wide open to a lot of assumptions as to exactly how your spreadsheet is set up.

    Show us what you have, by uploading a sample workbook, and what result/s you desire.
    Please consider:

    Be polite. Thank those who have helped you. Then Click on the star icon in the lower left part of the contributor's post and add Reputation. Cleaning up when you're done. If you are satisfied with the help you have received, then Please do Mark your thread [SOLVED] .

  3. #3
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    Re: Replace a number with text if it lies within a range/group

    Maybe my wording was not so great, hopefully I can illustrate it a little better.

    So using the following rules

    Range Designation
    1-9 A
    10-19 B
    20-29 C

    Column A:

    9
    15
    28
    7
    11

    would become:

    A
    B
    C
    A
    B

  4. #4
    Forum Contributor
    Join Date
    01-06-2012
    Location
    London, England
    MS-Off Ver
    2013
    Posts
    165

    Re: Replace a number with text if it lies within a range/group

    The solution I've used is:

    Please Login or Register  to view this content.
    Not the most efficient I'm sure, but it works nevertheless.

  5. #5
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    Hello BPSJACK,

    Or you could use this alternative,

    Please Login or Register  to view this content.

  6. #6
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    Or if you prefer the Case way, you could use,

    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Replace a number with text if it lies within a range/group

    (without VBA) with VLookup.

    See the attached file.

    Thanks Marcol.

    It seems the attachement wasn't uploaded!!
    Attached Files Attached Files
    Last edited by oeldere; 12-16-2012 at 01:27 PM. Reason: added: thanks marcol
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  8. #8
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Replace a number with text if it lies within a range/group

    Quote Originally Posted by Winon View Post
    Or if you prefer the Case way, you could use ...
    Try writing your Select statement like this
    Please Login or Register  to view this content.
    The Worksheet_Change should temporarily disable events to avoid calculation loops.
    i.e.
    Application.EnableEvents = False/True
    If you need any more information, please feel free to ask.

    However,If this takes care of your needs, please select Thread Tools from menu above and set this topic to SOLVED. It helps everybody! ....

    Also
    اس کی مدد کرتا ہے اگر
    شکریہ کہنے کے لئے سٹار کلک کریں
    If you are satisfied by any members response to your problem please consider using the small Star icon bottom left of their post to show your appreciation.

  9. #9
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    @ Marcol,

    I like that. Thank you!

  10. #10
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Replace a number with text if it lies within a range/group

    Or
    Please Login or Register  to view this content.

  11. #11
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Replace a number with text if it lies within a range/group

    Quote Originally Posted by jindon View Post
    Or
    Please Login or Register  to view this content.
    Hmm?
    Looks neat, but why introduce a loop? is it really required...

  12. #12
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Replace a number with text if it lies within a range/group

    Quote Originally Posted by Marcol View Post
    Hmm?
    Looks neat, but why introduce a loop? is it really required...
    You will see the difference when you enter/paste multiple cells in Col.A.

  13. #13
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    @ jindon,

    I cannot get your Code to work on my side.

    @ Marcol,

    And so we both forgot to add the "On Error Resume Next" line.

    Without it, highlight a Range in Column A, hit delete, and "Type Mismatch" Error appears.

  14. #14
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Replace a number with text if it lies within a range/group

    Working......
    Attached Files Attached Files

  15. #15
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    @ jindon,

    Thank you for sending me the WorkBook. It did not work on my side. Office 2007.

    Excel lost its mind! Exited completely, opened Excel again, and yes, now it works!

    Very sorry about that.

    Very impressive coding!
    Last edited by Winon; 12-17-2012 at 04:06 AM. Reason: Excel bombed out, causing confusion.

  16. #16
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Replace a number with text if it lies within a range/group

    Quote Originally Posted by jindon View Post
    You will see the difference when you enter/paste multiple cells in Col.A.
    Apologies, I must stop trying to read posts after leaving the pub ...

    Nice formula

    HLOOKUP() and VLOOKUP() were introduced, I think in 2003, maybe for all versions this line would be safer, if there are any oldies still using '97, and hey, it's shorter ...
    Please Login or Register  to view this content.

  17. #17
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Replace a number with text if it lies within a range/group

    V/HLookup function is available since xl 5.0 so no problem.

  18. #18
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    And to cater for Numbers/Data to be entered which do not fall within a range/group, I changed jindon's code as follows:

    Please Login or Register  to view this content.
    Hope you do not mind jindon.

  19. #19
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,525

    Re: Replace a number with text if it lies within a range/group

    If you do like that, you also need to care about numbers

    1) below <1
    2) with decimal

  20. #20
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: Replace a number with text if it lies within a range/group

    @ jindon

    Hi jindon,

    Thank you for your reply.

    If you do like that, you also need to care about numbers

    1) below <1
    2) with decimal
    The Code as it is handles that just fine.

+ 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