+ Reply to Thread
Results 1 to 22 of 22

VBA to remove text between to character (including those characters)

  1. #1
    Registered User
    Join Date
    10-31-2019
    Location
    Slovakia
    MS-Off Ver
    2016
    Posts
    14

    VBA to remove text between to character (including those characters)

    Hello, do you know about some simple VBA command, to remove everything between to characters including these characters? In this case it would be [random text], possibly also with space before [?
    I need to loop this through all items in column B, there might be 5 items, there might be 150 items, but always in column B.

    In pictures you can see example of before and after macro would be applied. Thank you for responses.

    Clipboard01.jpg
    Clipboard02.jpg
    Attached Files Attached Files
    Last edited by Hecky83150; 03-13-2020 at 06:37 AM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VBA to remove text between to character (including those characters)

    Please attach an EXCEL sample file ...!
    - Battle without fear gives no glory - Just try

  3. #3
    Forum Expert KOKOSEK's Avatar
    Join Date
    08-03-2018
    Location
    Pole in Yorkshire, UK
    MS-Off Ver
    365/2013
    Posts
    2,742

    Re: VBA to remove text between to character (including those characters)

    I don't know why no formula but if you want to:

    Please Login or Register  to view this content.
    Happy with my answer * Add Reputation.
    If You are happy with solution, please use Thread tools and mark thread as SOLVED.

  4. #4
    Registered User
    Join Date
    10-31-2019
    Location
    Slovakia
    MS-Off Ver
    2016
    Posts
    14

    Re: VBA to remove text between to character (including those characters)

    Quote Originally Posted by PCI View Post
    Please attach an EXCEL sample file ...!
    I'm sorry I'm getting error (javascript) fails, when I click to attachment nothing happens.

  5. #5
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: VBA to remove text between to character (including those characters)

    Try a bit longer version (but also working with something like blahblah [123] blahblah
    Please Login or Register  to view this content.
    PS as for attachment - have you followed this suggestion:
    Will you please attach a SMALL sample Excel workbook(s) (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually).

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.
    Best Regards,

    Kaper

  6. #6
    Registered User
    Join Date
    10-31-2019
    Location
    Slovakia
    MS-Off Ver
    2016
    Posts
    14

    Re: VBA to remove text between to character (including those characters)

    Quote Originally Posted by Kaper View Post
    Try a bit longer version (but also working with something like blahblah [123] blahblah
    Please Login or Register  to view this content.
    PS as for attachment - have you followed this suggestion:
    Will you please attach a SMALL sample Excel workbook(s) (10-20 rows of data is usually enough)? Please don't attach a picture of one (no-one will want to re-type all your stuff before starting).

    1. Make sure that your sample data are truly REPRESENTATIVE of your real data. (If trere are typical cases like: all unique values/duplicates could occur, day/night, nobody present/several persons at once, before/on/past due, etc. - please show them all or at least indicate in text) The use of unrepresentative data is very frustrating and can lead to long delays in reaching a solution.

    2. Make sure that your desired solution(s) is/are also shown (mock up the results manually).

    3. Make sure that all confidential/restricted information (either personal or business) like real e-mails, social security numbers, bank accounts, etc. is removed first!!

    To attach an Excel file you have to do the following: Just before posting, scroll down and press Go Advanced button and then scroll down and press Manage Attachments link. Now follow the instructions at the top of that pop-up screen.
    Thank you, I have managed to add attachment example now, and your code works well. I'm sorry I've tried it wrong way to put there example.

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

    Re: VBA to remove text between to character (including those characters)

    Try
    Please Login or Register  to view this content.

  8. #8
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: VBA to remove text between to character (including those characters)

    Probably
    Please Login or Register  to view this content.
    but as for dealing with different number (or non-present) spaces, cell-by-cell approach will be needed anyway

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

    Re: VBA to remove text between to character (including those characters)

    Quote Originally Posted by Hecky83150 View Post
    to remove everything between to characters including these characters? In this case it would be [random text], possibly also with space before [?
    Please Login or Register  to view this content.

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

    Re: VBA to remove text between to character (including those characters)

    You are welcome and thanks for the rep.

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBA to remove text between to character (including those characters)


    Hi,

    on a cell-by-cell approach Split VBA function is the easy way to go …

  12. #12
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VBA to remove text between to character (including those characters)

    For the fun another way ... Marc, beat me one click
    Please Login or Register  to view this content.

  13. #13
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,623

    Re: VBA to remove text between to character (including those characters)

    I'd still add trunc to it like:
    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VBA to remove text between to character (including those characters)

    I'd still add trunc to it like
    Yes that was concerning the last space, I did not want to care and it is nicer than the TRIM statement.
    Thx

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBA to remove text between to character (including those characters)

    Quote Originally Posted by PCI View Post
    For the fun another way ... Marc, beat me one click
    Salut PCI !

    Well, according to the attachment, it needs a single Split …

  16. #16
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,907

    Re: VBA to remove text between to character (including those characters)

    @ Kaper

    Why would you use Trunc ? Afaik Trunc is used to get the integer part of a number (like VBA Fix) but we're dealing with Text strings here ?
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  17. #17
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VBA to remove text between to character (including those characters)

    to follow Kaper's commentvRTRIM statement is a possibility
    Please Login or Register  to view this content.

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBA to remove text between to character (including those characters)


    As it needs only a single Split so On Error codeline is useless, according to the attachment …

  19. #19
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: VBA to remove text between to character (including those characters)

    On Error codeline is useless
    Well it is in case there is some records with out "["
    Can you show what will be a solution with a single split, just to understand and learn

  20. #20
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    According to the Excel VBA main coding rule I would first go like in post #7 …

    But for some reason if really a 'cell-by-cell approach' is needed
    according to the attachment the Split VBA function is obviously the easy way to go :

    PHP Code: 
    Sub Demo1()
        
    Dim VR&
        
    With [A1].CurrentRegion.Columns(2)
            
    = .Value2
            
    For 1 To UBound(V):  V(R1) = Split(V(R1), " [")(0):  Next
           
    .Value2 V
        End With
    End Sub 

  21. #21
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow

    Quote Originally Posted by PCI View Post
    Well it is in case there is some records with out "["
    Well that's very not a concern with the index #0 …

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

    Re: VBA to remove text between to character (including those characters)

    Quote Originally Posted by Kaper View Post
    Probably
    Please Login or Register  to view this content.
    but as for dealing with different number (or non-present) spaces, cell-by-cell approach will be needed anyway
    Assuming you only had to handle one or no space possibilities, you could simply do two global replacements instead iterating each cell individually...
    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. [SOLVED] Create a formula to remove last two characters from a cell if the 2nd character is a space
    By SellMeTv2 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-28-2019, 11:13 PM
  2. remove all text after and including the last ":" character in column c
    By smith310 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-10-2013, 09:36 AM
  3. Macros to remove a special character and all the characters after that....
    By Umapathy kumar in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-07-2013, 01:27 AM
  4. [SOLVED] remove all characters to right of "-" character, including "-"
    By maacmaac in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 04-27-2012, 02:43 PM
  5. Replies: 5
    Last Post: 08-29-2005, 11:05 PM
  6. Replies: 1
    Last Post: 04-18-2005, 05:06 PM
  7. Replies: 1
    Last Post: 04-17-2005, 08:10 PM

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