Results 1 to 12 of 12

Find and replace characters within alphanumeric string of varying lengths, with conditions

Threaded View

ExceLover2 Find and replace characters... 07-16-2019, 05:28 PM
jindon Re: Find and replace... 07-16-2019, 08:38 PM
dangelor Re: Find and replace... 07-16-2019, 09:11 PM
ExceLover2 Re: Find and replace... 07-17-2019, 04:51 AM
jindon Re: Find and replace... 07-17-2019, 05:00 AM
ExceLover2 Re: Find and replace... 07-17-2019, 05:29 AM
jindon Re: Find and replace... 07-17-2019, 05:32 AM
dangelor Re: Find and replace... 07-17-2019, 07:35 AM
ExceLover2 Re: Find and replace... 07-17-2019, 08:47 AM
dangelor Re: Find and replace... 07-17-2019, 01:43 PM
ExceLover2 Re: Find and replace... 07-17-2019, 04:26 PM
dangelor Re: Find and replace... 07-17-2019, 07:47 PM
  1. #1
    Registered User
    Join Date
    07-11-2019
    Location
    England
    MS-Off Ver
    2016
    Posts
    5

    Find and replace characters within alphanumeric string of varying lengths, with conditions

    Hi,

    I have data in column A that are alphanumeric strings which should always be in upper case and in any of the formats below (the question marks represent numbers that could be in any combination, and all other characters should always remain the same).
    94_00????_DMT_????
    94_00????_LOC_????
    94_00????_NME_????
    94_00????_PTN_????

    The data entered may be in any incorrect formats, including omitting any number of the _ or ? characters, such as in the examples below. This means that the length of the string varies, and the position of the characters needing to be replaced can also vary. The corrections need to be made until the last row of data.

    Incorrect entries:
    94_00????_LOC????
    94_00???DMT_???
    94_00????dmt???
    9400???_nme_??
    9400???_PTn_????
    94_00??_loc_????
    9400???PTN????

    I am using the code below to amend to upper case, correct the first part of the string if required, and to change the variations around "DMT" to just "DMT" and then to the correct format of "_DMT_". I have repeated this for the other formats.

    Columns("A:A").Select
        Selection.Replace What:="9400", Replacement:="94_00", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Selection.Replace What:="_DMT_", Replacement:="DMT", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Selection.Replace What:="_DMT", Replacement:="DMT", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Selection.Replace What:="DMT_", Replacement:="DMT", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
        Selection.Replace What:="DMT", Replacement:="_DMT_", LookAt:=xlPart, _
            SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
            ReplaceFormat:=False
    
      With Range("A1", Cells(Rows.Count, "A").End(xlUp))
            .Value = Evaluate("INDEX(UPPER(" & .Address(External:=True) & "),)")
        End With
    I only want 9400 to be changed in the first instance, what amendment do I need to make to the above code for this? Also, I am self teaching VBA and therefore using the record macro function to generate the code; in order to aid my understanding, is there more concise code or a better alternative I can use to acheive the same result?
    Last edited by AliGW; 07-17-2019 at 05:00 AM. Reason: Code tags added - remember next time, please.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How do I remove characters from cells of varying lengths?
    By bongofbobledore in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 10-08-2018, 03:42 AM
  2. Separating alpha and numeric characters, no delimiter, varying lengths
    By Trevor K in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 09-21-2016, 01:46 AM
  3. [SOLVED] Separating alpha and numeric characters, no delimiter, varying lengths
    By Trevor K in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 07-21-2015, 09:40 PM
  4. [SOLVED] copy alphanumeric character string of differing lengths to another column
    By karma2400 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-17-2014, 10:05 AM
  5. SUM based on two conditions in an array with varying lengths
    By jd354 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 12-06-2012, 06:38 PM
  6. Replies: 3
    Last Post: 05-09-2012, 11:36 AM
  7. Replies: 3
    Last Post: 02-21-2012, 09:34 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