Consulting

Results 1 to 10 of 10

Thread: How to: If First 12 Characters in line = "/", Delete character

  1. #1
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location

    How to: If First 12 Characters in line = "/", Delete character

    As title says, I am trying to scan just the first 12 characters in each line and if it contains a "/" delete that character. So for example:

    KB4024/AB
    AC3402/AB

    would become

    KB4024AB
    AC3402AB

    I am fairly new to vba but I'd imagine it would need to use arrays? Another way that seems lengthy is to use
    ActiveDocument.Paragraphs(n). _
    Range.Characters(m) and just keep looping around till m reaches 12. Would this method work?

    Thanks!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    You could use a wildcard Find/Replace, without the need for VBA...
    Find = ([^13^l][!^13^l/]{1,12})/
    Replace = \1
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Regular
    Joined
    Jul 2014
    Posts
    14
    Location
    Didn't know about wildcards =X. Simple solutions to simple problems I suppose haha. Thanks

  4. #4
    VBAX Regular
    Joined
    Apr 2010
    Posts
    28
    Location
    Dim TargetString as String. ' the string to be searched
    Dim Location as Integer
    Dim ResultString as String. ' the string without the /

    Location = Instr(TargetString, "/")

    If Location = 0 Or Location >12 Then
    ResultString = TargetString
    ' Because the / wasn't found (0) or
    ' wasn't in the first 12 characters (>12)
    Else
    ResultString = Left(TargetString, Location-1) & Right(TargetString, Len(TargetString) - Location)
    End If


    Geoff

  5. #5
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Geoff: A straightforward Find/Replace would be far faster. The longer the document, the faster it would be by comparison.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  6. #6
    If you want Paul's replace function in a macro then

    Dim oRng As Range
    Set oRng = ActiveDocument.Range
    With oRng.Find
    Do While .Execute(FindText:="([^13^l][!^13^l/]{1,12})/", _
    ReplaceWith:="\1", _
    MatchWildcards:=True)
    oRng.Collapse wdCollapseEnd
    Loop
    End With

    should do the job.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Graham,

    There's no need for the loop:
    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "([^13^l][!^13^l/]{1,12})/"
        .Replacement.Text = "\1"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = True
    End Sub
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  8. #8
    Indeed - more than one way to skin a cat
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Regular
    Joined
    Apr 2010
    Posts
    28
    Location
    Maybe I took the question too literally. I thought he only wanted to replace the / if it occurred in the first twelve characters and not if it was present further into the string.

    Geoff

  10. #10
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The F/R expression and code I've posted does only replace the / if it occurs in the first twelve characters. It ignores whatever may be further along.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

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