+ Reply to Thread
Results 1 to 22 of 22

Replace 6-digit text numbers

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Replace 6-digit text numbers

    I have a column populated by text fields, such as "Contract #4521897" or "Mandy Jone", which are descriptive and I want to keep. Also within the column are text numbers such as 015643 or 957351, which are not descriptive. I want to replace only the 6-digit text numbers. How do I do this via vba?

    Thanks,
    John

  2. #2
    Forum Expert
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    3,054

    Re: Replace 6-digit text numbers

    What do you want to replace them with? Are the numbers you want to replace always in a cell by themselves? If so you could use a formula, rather than having to mess around with VBA.

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Andrew,
    Sorry for not explaining further. This is a small routine in a much larger process that I'll be going through every day. It starts out with 12 workbooks, each having a worksheet with between 20K and 60K rows of data, in 68 columns. In ONE particular column in each of these workbooks I want to replace the 6 digit text numbers with something (I haven't determined what that "Something" is yet; maybe just text saying "No usable info here").

    Since there's so many workbooks, and so many rows, and I'll be doing this every morning (part of "normalizing data"), I've got the whole operation set to run by macros, but I'm stumped at how to configure the "Replace" piece.

  4. #4
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Replace 6-digit text numbers

    hi, Jomili, may be sort of this would help, run code "test"
    Attached Files Attached Files
    Last edited by watersev; 09-08-2011 at 05:45 PM.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Watersev,

    It works, but I sure don't understand how. Can you explain it to me?

  6. #6
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Update: it works, but not exactly. IF I have 6 characters, such as SixDig, it doesn't replace (which is the behavior I want) but if I have a number such as 456789123, it replaces the first 6 digits (which is a behavior I don't want).

  7. #7
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Replace 6-digit text numbers

    hi, Jomili, another guess as still there is no sample data available, please check attachment
    Attached Files Attached Files
    Last edited by watersev; 09-10-2011 at 01:34 PM.

  8. #8
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Watersev,

    I apologize that I didn't post any sample data; that was thoughtless of me. But I've tried your macro on my sample data and it appears to work flawlessly. My data is a combination of Text strings, and Text and non-text numerical sequences. I simply want to change any string of only six numbers to a common phrase, and it looks like you've solved the "how to" part of it. It would help me if I could understand how you did it. Would you care to explain?

    Thanks,
    John

  9. #9
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace 6-digit text numbers

    Based on Watersev's example:

    Please Login or Register  to view this content.



  10. #10
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Replace 6-digit text numbers

    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    SNB,
    Good try, but no go. I've attached my sample data. This is supposed to be a "Description" of expense. The 6 digits numbers contained in this mess convey no usable information, so I want to take them out (replace with "Whatever"). When I tried your macro, it not only replaced the 6 digit numbers, but the other numbers as well.
    Attached Files Attached Files

  12. #12
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace 6-digit text numbers

    Even simpler:

    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Almost. I realize I didn't include any in my sample, but frequently these are text numbers. Your last code doesn't work on text numbers.

  14. #14
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Watersev,

    I missed your post where you stepped through all the steps. Thank you very much for doing that. I'll have to study this.

  15. #15
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace 6-digit text numbers

    A precise description of a question is 95% of the solution.

    Please Login or Register  to view this content.

  16. #16
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Hmmm,

    This one is pasting #VALUE! in all cells in column C when I run it against my test data.

  17. #17
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace 6-digit text numbers

    So you forgot to remove the last bracket....

    Please Login or Register  to view this content.

  18. #18
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Well, I guess I did forget to remove the last bracket. Thanks for straightening that out for me. I've tested your last code and it works fine on both text and "normal" numbers. However, for lines having a different text string ("# 232710 Dome Notary Seal Lo") I'm getting the value error. What can we do about that?

  19. #19
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Replace 6-digit text numbers

    Hello jomili,

    Here is VBA solution that works. I have added a button to the sheet to run the macro. Here is the macro code.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  20. #20
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace 6-digit text numbers

    Now we can combine:

    Please Login or Register  to view this content.

  21. #21
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    3,952

    Re: Replace 6-digit text numbers

    Leith,
    Good job! Got it on the first try!

    Both yours and Watersev's solutions make use of the "RegExp" object, and use "Pattern", which I haven't encountered before. Something else I need to study up on.

    SNB,
    Excellent work! Your last attempt does the trick.

    Now my dilemma is which of 3 equally good routines to use. Oh well, that's my problem. Thanks to all 3 of your for the help and showing me how much I still need to learn.

    Thanks,
    John

  22. #22
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Replace 6-digit text numbers

    I think I found the oneliner I was looking for:

    Please Login or Register  to view this content.

+ 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