+ Reply to Thread
Results 1 to 10 of 10

Simplified Search and Replace

  1. #1
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Simplified Search and Replace

    Hi All

    In H6:H300 I have text in the form

    01/04/2018..... (F32)
    25/04/2018..... (F37)
    03/05/2018..... (F40)
    11/05/2018..... (F44)
    16/05/2018..... (F46)
    04/07/2018..... (F62)
    10/07/2018..... (F64)
    31/07/2018..... (F71)
    08/08/2018..... (F74)


    I'm trying via VBA to search and replace the first number to include ordinal suffixes , and to replace the second number with the month in full. Spaces would replace obliques.

    So the above would become :

    1st April 2018..... (F32)
    25th April 2018..... (F37)
    3rd May 2018..... (F40)
    11th May 2018..... (F44)
    16th May 2018..... (F46)

    and so on.

    I think a really really long search and replace routine would do it , but I'm hoping it can be done with something much neater and quicker.

    I don't know if it's a complicating factor but H6:H300 are merged with their neighbours in column I. All the relevant cells have General formatting.

    Grateful for any advice.
    Last edited by CDandVinyl; 01-13-2020 at 08:14 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    12-22-2015
    Location
    HK
    MS-Off Ver
    2010
    Posts
    532

    Re: Simplified Search and Replace

    Please Login or Register  to view this content.
    Last edited by ikboy; 01-14-2020 at 12:16 AM.

  3. #3
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Simplified Search and Replace

    Many thanks for this. I'm beyond impressed , I have to say.

    It's working perfectly, although it gives ordinals 21th and 31th.



    BTW - Strangely , when I run the code as a separate macro , it runs through cleanly. When I incorporate the code into an existing macro (minus the 'Sub zz()' and 'End Sub' lines) , it gives an 'unknown' error for variable i. I added i to the Dim list and it runs fine. Curious....

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

    Re: Simplified Search and Replace

    Meaningless use of Regular Expressions...
    Try
    Please Login or Register  to view this content.

  5. #5
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Simplified Search and Replace

    Quote Originally Posted by ikboy View Post
    Please Login or Register  to view this content.
    What I highlighted in red above can be replaced by this single line of code (which will work correctly for any number n, not just a date's day value)...

    s =n & Mid$("thstndrdthththththth", 1 - 2 * (n Mod 10) * (Abs(n Mod 100 - 12) > 1), 2)



    Quote Originally Posted by jindon View Post
    Meaningless use of Regular Expressions...
    Try
    Please Login or Register  to view this content.
    Similar to my above comment to ikboy, the above red highlighted code can be replaced by this single line of code if desired...

    sufx = Mid$("thstndrdthththththth", 1 - 2 * (x(0) Mod 10) * (Abs(x(0) Mod 100 - 12) > 1), 2)



    NOTE
    -----------------------
    For those who might be interested, given the way Excel's MOD function handles negative numbers, the ordinal suffix for a number (any number, not just a date's day number) in cell A1 can be calculated using this "simpler" expression for a formula in a cell...

    =A1&MID("thstndrdth",MIN(9,2*RIGHT(A1)*(MOD(A1-11,100)>2)+1),2)

    where the part highlighted in blue includes the number itself (so if A1 contains 11, the formula returns 11th, not just th).
    Last edited by Rick Rothstein; 01-14-2020 at 02:36 AM.

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

    Re: Simplified Search and Replace

    So what's wrong???

    I use Switch/Choose function instead, if I need to change...

  7. #7
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,416

    Re: Simplified Search and Replace

    I didn't say anything was wrong... I was just providing an alternative for future readers who might find what I posted interesting. For the record, I do not like VB's Switch and Choose functions as they are fairly "slow" functions compared to their multi-line equivalents (plus one has to be wary if using expressions that might produce an error). By the way, in case you might be interested, I added an Excel formula equivalence (shorter than the VB version) as a note to Message #5 after you had posted your comment in Message #6. And in case you don't find it interesting, my thought is that a future reader of this thread might.
    Last edited by Rick Rothstein; 01-14-2020 at 02:46 AM.

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

    Re: Simplified Search and Replace

    No need to refer my codes, just post your solution, if you want to.

    Nothing wrong with Select case statement in speed. One liner is not always fastest all the time.

  9. #9
    Forum Contributor
    Join Date
    01-18-2013
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    641

    Re: Simplified Search and Replace

    Please Login or Register  to view this content.


    Just for my own education , which element of the code changes the numbers below 10 to one digit rather than two?

    Before running the code the numbers are

    01 , 02 , 03 , 04

    and so on.

    After the code they are

    1st , 2nd , 3rd , 4th.

    How could the code be modified to be 01st , 02nd , 03rd ....?

    Just curious.

    Thanks for your time and considerable expertise.

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

    Re: Simplified Search and Replace

    Val function converts number as string to numeric numbder, so beginning 0 will be removed.
    If you remove Val function, it gives you 2 digit numbers as they are.
    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)

Similar Threads

  1. Search and replace Text Replace Formula
    By Vonblack in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 05-21-2019, 11:30 AM
  2. Macro: Search and replace: Replace using a cell reference
    By kalyan46 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-11-2019, 07:11 PM
  3. [SOLVED] How to replace specific text search for any date search
    By katiedee1625 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-08-2018, 11:22 AM
  4. [SOLVED] Search and Replace.
    By manharji in forum Excel General
    Replies: 9
    Last Post: 07-30-2012, 03:34 PM
  5. Search and replace VB with VB
    By Mayweed in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-14-2011, 08:02 AM
  6. Replies: 4
    Last Post: 08-03-2006, 02:00 PM
  7. Search and replace
    By Subu in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-06-2005, 07:05 AM

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