Results 1 to 11 of 11

Thread: Delete everything after a "-"[RESOLVED]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Posts
    168

    Resolved Delete everything after a "-"[RESOLVED]

    I would like to delete everything after a "-" in a textbox. How would I do this? Please help if you can. Thank You.
    Last edited by andy345; Feb 10th, 2005 at 09:04 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Delete everything after a "-"

    Depending on the event you want this to occur in, you would use something like this.
    VB Code:
    1. Text1.Text = Mid$(Text1.Text, 1, InStr(1, tText1.Text, "-") - 1)
    Also, you would want to place some error trapping in there too in case there is no "-" found.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Delete everything after a "-"

    VB Code:
    1. text1.text = Left(text1.text,instr(text1.text,"-")-1)

    Will work as long as there is a - in the text. it will fail otherwise, so you should maybe use an error trap.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Delete everything after a "-"

    You need to do a -1 if you dont want the dash.
    Quote Originally Posted by dglienna
    text1.text = Left(text1.text,instr(text1.text,"-"))
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Posts
    168

    Re: Delete everything after a "-"

    Ok I think I messed up what I wanted to say I want to take everything before the "-" and put it in text2 and leave text1 the way it is. so if text1 is yay - text
    text2 will become yay and text1 will stay the same

  6. #6
    Addicted Member Max_aka_NOBODY's Avatar
    Join Date
    Jul 2004
    Location
    Amman, Jordan
    Posts
    179

    Re: Delete everything after a "-"

    IT's even simpler then.

    VB Code:
    1. Text2.Text = Mid$(Text1.Text, 1, InStr(1, tText1.Text, "-") - 1)

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Delete everything after a "-"

    ROB - I realizzed that when we both posted at the same time. Instead of blindly following, I pasted it into the IDE to see that I had made an error, and I changed it. We posted at the same time.

    Andy, this will do it.

    VB Code:
    1. text2.text = Left(text1.text,instr(text1.text,"-")-1)

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Delete everything after a "-"

    Oh, you must of edited it quite quick. It doesnt show "Last edited by dglienna at xx:xx:xx"

    Down to the same minute
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Delete everything after a "-"

    I've noticed that it doesn't show up if you edit the post right away. I tend to do that a lot. I thought the difference may have been using MID as opposed to left, and had the IDE open anyways. Just created a textbox and a button, pasted in the code, added "- asfhhl" to the word "Text" and hit the button. Should have tested first, once again.

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2004
    Posts
    168

    Re: Delete everything after a "-"[RESOLVED]

    Thank You all I got it to work with dglienna's post of
    VB Code:
    1. text2.text = Left(text1.text,instr(text1.text,"-")-1)

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Delete everything after a "-"[RESOLVED]

    Glad to hear it. Keep coming back!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width