Consulting

Results 1 to 9 of 9

Thread: Solved: Remove certain charachters from a cell

  1. #1
    VBAX Regular
    Joined
    Feb 2013
    Posts
    30
    Location

    Solved: Remove certain charachters from a cell

    Hi,
    I have a worksheet column in which few of the cells have some unwanted characters and I wanted to remove those unwanted characters from these cells like
    PK_TTA121 in this remove "PK_" and change this to TTA121
    PK_TLU955CV in this remove "PK_" & "CV" and change this to TLU955
    PK_LSB6882C in this remove "PK_" & "C" and change this to LSB6882
    TTB966 no change
    TLS5263C in this remove "C" and change this to TLS5263
    Would be grateful if somebody help me in writhing the code to do this automatically. I am also attaching the example sheet
    Regards
    Attached Files Attached Files

  2. #2
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    [VBA]Sub aa()
    LR = Cells(Rows.Count, "E").End(xlUp).Row
    For j = 2 To LR
    st = Cells(j, 5)
    pos = InStr(st, "_")
    If pos > 0 Then
    st = Right(st, Len(st) - pos)
    End If
    For i = Len(st) To 1 Step -1
    If Not IsNumeric(Mid(st, i, 1)) Then
    st = Left(st, i - 1)
    Else
    Exit For
    End If
    Next
    Cells(j, 6) = st
    Next
    End Sub[/VBA]

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,646
    [VBA]
    sub M_snb()
    cells.replace "PK_",""
    cells.replace "CV",""
    cells.replace "C",""
    end sub
    [/VBA]

  4. #4
    VBAX Regular
    Joined
    Feb 2013
    Posts
    30
    Location
    Thanks Patel & snb, for your valuable help. Unfortunately the code provided by Patel didn't work in my file. But the code provided by snb worked fine except it removed "C" from the original tank lorry number instead I wanted "C" at the end to remove example
    TTC966 no change
    TTB966C to be changed to TTB966

  5. #5
    VBAX Regular
    Joined
    Feb 2013
    Posts
    30
    Location
    sorry for inconvenience

  6. #6
    VBAX Mentor
    Joined
    Jul 2012
    Posts
    398
    Location
    my code works on attached workbook, if you want use it on another you have to arrange it or attach the real workbook

  7. #7
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    sarfaraz,

    you should provide a "pattern" for this type of string manipulation.

    if it is up to me, i can say, you want to extract all the numbers and 3 letters before the first occurence of a number from a string.

    is this the case?

    or are there any strings such as X12345678, XX98645, etc (X being any letter)?

    to be more specific... do the strings always contain first 3 (or more) letters and then numbers?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  8. #8
    In that case you might need to use something like this.
    Assuming the strings are in A2 and down.

    [VBA]
    Sub Try_This()
    Dim c As Range
    Application.ScreenUpdating = False
    For Each c In Range("A2:A" & Cells(Rows.Count, 1).End(xlUp).Row)
    c.Replace "PK_", ""
    c.Replace "CV", ""
    If Mid((c.Value), Len(c.Value), 1) = "C" Then c.Value = Left(c.Value, Len(c.Value) - 1)
    Next c
    Application.ScreenUpdating = True
    End Sub
    [/VBA]

  9. #9
    VBAX Regular
    Joined
    Feb 2013
    Posts
    30
    Location
    jolivanes, thanks it worked perfectly alright

Posting Permissions

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