Closed Thread
Results 1 to 6 of 6

Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

  1. #1
    Registered User
    Join Date
    10-23-2012
    Location
    Asheville, NC
    MS-Off Ver
    MS Office 2011 for Mac
    Posts
    8

    Question Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

    So I have this code that is supposed to find a string that looks like this: "Op.123" Once it finds this string, it performs a cut/paste that puts the string at the beginning of the cell, adds a comma, and adds a space. For example, if you have the string: This is my Op.123 string" the code should change it to: "Op. 123, This is my string". The problem is, it only works when the string is at the beginning of the cell text but doesn't work anywhere else. So "Op. 123 string" works but "String Op.123" does not work. I am working on a Mac so any code needs to work with the Mac version of Excel. Here is the code that needs to be tweaked:

    HTML Code: 
    Thanks in advance. You guys are awesome
    Last edited by Jason_McCoy; 04-03-2019 at 08:12 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,717

    Re: Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

    Here is a solution that uses regular expressions, which is a much more powerful pattern matching tool than Like and InStr. It allows you to find out if a pattern is present, capture it, and switch it around. I did some testing to make sure the concept works but without your file and data I can't test this exact code.

    Please Login or Register  to view this content.
    Regex is a pattern matching language. I will break down the pattern I used:

    ^(.*)(Op\.[0-9]*)(.*)$

    ^ matches the beginning of the line. It ensures that the pattern must start at the beginning of the line.

    (.*) The . means "match any character". The * means "match any number of the preceding character" so it will match any string of characters. The ( ) mean "save whatever matches this".
    (Op\.[0-9]*) This says look for Op. (you have to use the \ to mean "look for an actual . instead of matching any character") followed by any number of digits. Save whatever matches this.
    (.*)$ The $ means "end of string" so this will match all characters following your Op string to the end of the line. Save whatever matches this.

    Now we have saved three strings: The beginning the string, the Op.xxx string, and the end of the string. We can use the $ variables to rearrange them. Each string we saved can be referenced in Replace as $1, $2, and $3. So we replace the above string with

    $2, $1$3

    $2 is the Op string, followed by the original beginning of the string, and then the original end of the string.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    10-23-2012
    Location
    Asheville, NC
    MS-Off Ver
    MS Office 2011 for Mac
    Posts
    8

    Re: Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

    As I mentioned if my post, I am working on a Mac. That code will not work with a Mac version of Excel.

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

    Re: Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

    https://www.excelforum.com/excel-pro...same-cell.html
    Please Login or Register  to view this content.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,717

    Re: Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

    Quote Originally Posted by Jason_McCoy View Post
    As I mentioned if my post, I am working on a Mac. That code will not work with a Mac version of Excel.
    Sorry, I thought it was platform-independent.

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,717

    Re: Find Substring Cut and Paste to Beginning of Same Cell (Almost Works....)

    Administrative Note:

    Welcome to the forum.

    Unfortunately, this is a duplicate thread, and you are allowed only ONE thread per issue here. I am, therefore, closing this thread, but you may continue here in the original thread

Closed Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Find Substring Cut and Paste to Beginning of Same Cell
    By Jason_McCoy in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 03-29-2019, 09:06 PM
  2. Replies: 8
    Last Post: 03-09-2018, 11:39 AM
  3. Macro to search cells for substring and replace contents of cell if substring is found
    By robbyvegas in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-31-2015, 06:40 AM
  4. Find duplicate substring within same cell
    By kingoftheace in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 02-11-2015, 05:04 PM
  5. Loop - Find substring, Change cell value
    By Aardvark1971 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 12-31-2014, 10:53 PM
  6. VBA Code to Cut the Last Character of A cell and Paste It at The Beginning
    By whallgren in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-19-2010, 04:30 AM
  7. Find the sub string in column and copy the cell next to the substring
    By shrujan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-16-2009, 11:00 AM

Tags for this Thread

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