+ Reply to Thread
Results 1 to 15 of 15

Macro to delete characters between 2 characters.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Macro to delete characters between 2 characters.

    HI I need help to make a macro to delete all characters between 2 characters.
    Thanks to PCI I have sorted allot out all ready, but need this general last thing.
    I Need a macro which will delete all characters after 74 from the left and the second - from the right.

    If I can have that I will manage to delete what I don't need.
    In below example I need this to be deleted which are in red.
    C:\Documents and Settings\Alancecille\Desktop\torrents\Karaoke songs\ 0002 --thm - Ig Joe Turner - Shake Rattle & Roll.zip
    C:\Documents and Settings\Alancecille\Desktop\torrents\Karaoke songs\ 0003 ps12520 - Jerry Lee Lewis - Great Balls Of Fire.zip
    C:\Documents and Settings\Alancecille\Desktop\torrents\Karaoke songs\ 0004 jkdjk -- - Jimmy Buffett - Margaritaville.zip

    P.s The macro has to work in column B start from B2 all the way down


    Please have a look. I need a macro because it have to be used with other macros.

    Thanks in advance

    Sincerely
    Abjac
    Last edited by abjac; 09-29-2014 at 04:17 AM.

  2. #2
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to delete characters between 2 characters.

    strResult = mid(strInput,1,firstchar) & mid(strInput,firstchar+2,len(strinput)-firstchar+2)
    Hope this helps

    Sometimes its best to start at the beginning and learn VBA & Excel.

    Please dont ask me to do your work for you, I learnt from Reading books, Recording, F1 and Google and like having all of this knowledge in my head for the next time i wish to do it, or wish to tweak it.
    Available for remote consultancy work PM me

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to delete characters between 2 characters.

    Hi,

    Assuming column C is blank and can be used as a temporary working column

    Sub DeleteCharacters()
        Dim lLastrow As Long, st As String, x As Long
        lLastrow = Range("B" & Rows.Count).End(xlUp).Row
    
    
        Range("C1:C" & lLastrow) = "=LEFT(B1,74)&RIGHT(B1,LEN(B1)-FIND(""|"",SUBSTITUTE(B1,""*- "",""|"",1)))"
        Range("C1:C" & lLastrow).Copy
        Range("B1").PasteSpecial (xlPasteValues)
        Range("C1:C" & lLastrow).Clear
    End Sub
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Macro to delete characters between 2 characters.

    HI Nathansav and thanks allot. Could you please show the full code,
    And also if it could be made so it take from 74 from the left, in this case I can find out change it if nessasary.

    Thanks in advance

    Abjac
    Last edited by abjac; 09-29-2014 at 04:34 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to delete characters between 2 characters.

    That is the full code.
    Last edited by JBeaucaire; 09-29-2014 at 10:24 AM.

  6. #6
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Macro to delete characters between 2 characters.

    HI I put it like this but it don't delete anything.

    Sub test()
    Sheets(1).Activate
    
    strResult = Mid(strinput, 1, firstchar) & Mid(strinput, firstchar + 2, Len(strinput) - firstchar + 2)
    End Sub
    I have put the test sheet in here now. Please have a look thanks
    All in read should be deleted
    Abjac
    Attached Files Attached Files

  7. #7
    Valued Forum Contributor
    Join Date
    09-21-2011
    Location
    Birmingham UK
    MS-Off Ver
    Excel 2003/7/10
    Posts
    2,188

    Re: Macro to delete characters between 2 characters.

    You have to put your title in the strInput string, and first char is what ever number you want to do it from..

  8. #8
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Macro to delete characters between 2 characters.

    Try:

    Sub a()
        Dim s
        s = "C:\Documents and Settings\Alancecille\Desktop\torrents\Karaoke songs\ 0003 ps12520 - Jerry Lee Lewis - Great Balls Of Fire.zip"
        MsgBox Left(s, 75) & Mid(s, InStrRev(s, "-", InStrRev(s, "-") - 1))
    End Sub
    多么想要告诉你 我好喜欢你

  9. #9
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Macro to delete characters between 2 characters.

    Hi All. Maybe its me write it wrong the string
    C:\Documents and Settings\Alancecille\Desktop\torrents\Karaoke songs\ 0002 --thm - Ig Joe Turner - Shake Rattle & Roll.zip
    Is not a string I shall open, its how it stand in the cell. Its extracted to column B from the folder, so the path is put in column as a string text. That text I need to change and after I need to change the file name.

    Nathansav. sorry maybe I am lost hehe. But please include if you know the full code.
    It has to work from column B2 and down. So it delete after the criteria in the string in every cell

    Thanks in advance

    Abjac

  10. #10
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Macro to delete characters between 2 characters.

    HI Richard and thanks. Just tested it. In the test sheet I uploaded it delete what I have in cell B2 to B3 and all there is left is words #VALUE!. I changed to B2 and C2 because it start there. But also tried without that from cell B1. I use excel 2003.

    Thanks

    Abjac

  11. #11
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to delete characters between 2 characters.

    Hi,

    I'm attaching the workbook I used for testing. (Sheet1). Check this out first and if this works and yours doesn't then upload the workbook you're using
    Attached Files Attached Files

  12. #12
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Macro to delete characters between 2 characters.

    Thanks Richard yours is working but the weird thing is when I pasted your code in to my test sheet it don't work. But I saved yours as a xls file and its working. But if you download upper file I uploaded here. It will not work there. I cant find out why really, its really strange. But its working your for sure so thanks allot.

    Is it difficult after to add a space between the 4 digits and the - Like this 0002 - xxxxx, It come out with this now 0002- xxxxx.
    But try download my first sheet i uploaded here its really weird its not working there.

    I changed the code to fit that it start from row 2 like this.
    Sub DeleteCharacters()
        Dim lLastrow As Long, st As String, x As Long
        lLastrow = Range("B" & Rows.Count).End(xlUp).Row
    
    
        Range("C2:C" & lLastrow) = "=LEFT(B2,74)&RIGHT(B2,LEN(B2)-FIND(""|"",SUBSTITUTE(B2,"" - "",""|"",1)))"
        Range("C2:C" & lLastrow).Copy
        Range("B2").PasteSpecial (xlPasteValues)
        Range("C2:C" & lLastrow).Clear
    
    End Sub
    But have a look please on the sheet I uploaded and if you can fix the space it would be great.

    Thanks

    Sincerely
    Abjac

  13. #13
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to delete characters between 2 characters.

    Hi,

    See attached.

    Any problems let me know.
    Attached Files Attached Files

  14. #14
    Forum Contributor
    Join Date
    07-28-2012
    Location
    madrid
    MS-Off Ver
    Excel 2010 at work excel 2016
    Posts
    1,102

    Re: Macro to delete characters between 2 characters.

    HI Richard it works like a charm great. Thanks allot for being patience and help with this. Great help

    Have a nice day.

    Sincerely
    Abjac

  15. #15
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to delete characters between 2 characters.

    My pleasure to have helped and thanks for the rep.

+ 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. Macro to delete row if the first characters are numbers
    By Ilwuen in forum Excel Programming / VBA / Macros
    Replies: 16
    Last Post: 05-20-2014, 05:14 PM
  2. [SOLVED] Delete characters and add * in front and last characters in ACCESS QUERY
    By LALTIZER in forum Access Tables & Databases
    Replies: 9
    Last Post: 02-01-2013, 02:06 PM
  3. [SOLVED] Characters.delete only works with less than 255 characters?
    By mexindian in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 09-26-2012, 11:57 AM
  4. Macro to Delete all but first 3 characters
    By megdawom in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2011, 07:46 PM
  5. macro to delete all characters in a row before @ character
    By chcal301 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-24-2005, 04:49 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