Consulting

Results 1 to 7 of 7

Thread: VBA remove specific characters in cell

  1. #1

    VBA remove specific characters in cell

    Hi all,

    I have this data


    Reg 01 46107 20JUN2016
    Reg 01 45307 20MAY2016
    Reg 01 44493 20APR2016
    Reg 01 43621 20MAR2016
    Reg 01 42786 20FEB2016

    Now at first each line is in 1 cell. eventually in my VBA code i get it to text to columns it.

    Now, i need to remove Reg at the start. The reason i'm finding this hard is because sometimes the data starts with just an R and sometimes nothing at all - just the number. Most of the time there is no space between the R and the numbers which is why i've only just discovered this problem...

    Any ideas?

    Cheers

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    I'm just beginning to dabble with Excel so I'm not sure if I understand your question or about the efficiency of this proposed solution.

    If you want to trim "Reg" from the start of each cell where it occurs then perhaps his will do:

    Public Sub TrimRegFromCell()
    Dim cell As Range
      For Each cell In ActiveSheet.UsedRange
        If Left(cell.Value, 3) = "Reg" Then
          cell.Value = Mid(cell.Value, 4, Len(cell.Value) - 3)
        End If
      Next
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    As a UDF

    Function JustData(s As String) As String
        If Left(s, 4) = "Reg " Then
            JustData = Right(s, Len(s) - 4)
        ElseIf Left(s, 3) = "Reg" Then
            JustData = Right(s, Len(s) - 3)
        ElseIf Left(s, 2) Like "R[0-9]" Then
            JustData = Right(s, Len(s) - 1)
        Else
            JustData = s
        End If
    End Function

    Could in turned into a sub and might be a little faster
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,645
    Sub M_snb()
         sheet1.cells.replace "Reg ",""
    End Sub

  5. #5
    snb, i think for now this will do. I'm sure i'll come across so much more data formats that'll throw things back up again haha!

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    sometimes the data starts with just an R and sometimes nothing at all - just the number. Most of the time there is no space between the R and the numbers

    I think that it only gets the easy ones
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    I think you're right, i'll stick your code in later

Posting Permissions

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