Results 1 to 8 of 8

Thread: [RESOLVED] Remve everything in cell after specific full word + word (VBA)

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [RESOLVED] Remve everything in cell after specific full word + word (VBA)

    Hi,

    I'm in need of a VBA code that removes everything in a cell after a specific word + the word itself in the active sheet and/or selection. Perhaps this can be done in 2 separate codes. (one for full sheet and one for selection)

    In the following example we want to remove the "word_to_replace" and everything after that. We are going to search for the word "or":
    VBA or VB
    VBA OR C#
    C# ororor or C++

    After executing the code in the current active sheet I want the result to be:

    VBA
    VBA
    C# ororor

    Anyone?

    Thanks in advance.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Remve everything in cell after specific full word + word (VBA)

    Code:
    ssrch = " or "    ' note spaces
    for each cel in selection.cells
      pos = instr(cel, ssrch, vbtextcompare)
      if pos > 0 then cel.value = left(cel, pos -1)
    next
    for whole sheet,
    replace selection.cells with activesheet.cells
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Remve everything in cell after specific full word + word (VBA)

    Thanks

    Unfortunalety I get type mismatched (13)

    pos = InStr(cel, ssrh, vbTextCompare)

    This is my code:

    Code:
    Option Explicit
    
    Private Sub CommandButton1_Click()
        Dim ssearch As String
        Dim cel As Range
        Dim pos As Integer
        
        ssearch = " " & Trim(TextBox1.Text) & " "   ' note spaces
        For Each cel In Selection.Cells
            pos = InStr(cel, ssearch, vbTextCompare)
            If pos > 0 Then cel.Value = Left(cel, pos - 1)
        Next
    End Sub


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  4. #4
    PowerPoster
    Join Date
    Oct 2008
    Location
    Midwest Region, United States
    Posts
    3,574

    Re: Remve everything in cell after specific full word + word (VBA)

    Try something like this:

    Code:
    For Each c In rng
            pos = InStr(1, c.Value, "or")
        Next
    The first argument for InStr is "start", so you wouldn't want "cel" there, I don't think.

    Pete, am I understanding that correctly, or no?

  5. #5

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Remve everything in cell after specific full word + word (VBA)

    Code:
    pos = InStr(1, cel, ssearch, vbTextCompare)
    seems to work.

    There is a small issue left. When using ActiveSheet it also searches in the empty cells, which takes a long time. Any way to avoid that?
    Already using application.screenupdating btw.
    Last edited by Radjesh Klauke; Oct 12th, 2012 at 04:41 PM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Remve everything in cell after specific full word + word (VBA)

    Code:
    if not isempty(cel) then
            pos = InStr(1, cel, ssearch, vbTextCompare)
            If pos > 0 Then cel.Value = Left(cel, pos - 1)
    end if
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Remve everything in cell after specific full word + word (VBA)

    Thanks, but unfortunately it doesn't work. The computer freezes completely and it takes more then 5 hours before it's done...

    Code:
        Dim ssearch As String
        Dim cel As Range
        Dim pos As Integer
    
        ssearch = " " & Trim(txt_word.Value) & " "
    
        Application.ScreenUpdating = False
        Application.Calculation = xlCalculationManual
    
         For Each cel In Selection.Cells
            If Not IsEmpty(cel) Then
                     ...
            End If
         Next
    
    ...
    Last edited by Radjesh Klauke; Oct 14th, 2012 at 04:14 AM.


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  8. #8

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Re: Remve everything in cell after specific full word + word (VBA)

    Did some more thinking and figured it out
    Code:
    For Each cel In ActiveSheet.UsedRange
    Did the trick.

    Thanks for a the help. Much appreciated. +REP


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

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