Consulting

Results 1 to 10 of 10

Thread: Solved: remove signs (wildcards) from a string

  1. #1
    VBAX Mentor
    Joined
    Nov 2008
    Posts
    305
    Location

    Solved: remove signs (wildcards) from a string

    I have some code which creates a new folder, with a name based on a string from user input.

    However before I can use the string provided by the user I need to strip the string of any invalid characters (such as \/:*?"<> and |)

    Does anyone have a snip of code that can quickly do this for me?

    Thanks

  2. #2
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    If the string comes from a textbox you can validate it there:

    [VBA]
    Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If TextBox1.Value > 100 Then
    Cancel = True
    Me.TextBox1.SelStart = 0
    TextBox1.SelLength = Len(TextBox1.Text)
    MsgBox "Error"
    End If
    End Sub
    [/VBA]

    just an example.....replace the > 100 with = your wildcard charactors.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    [VBA]Option Explicit

    Public Sub Example()
    Dim test As String
    test = "I\Am/A?Test"
    MsgBox StripChars(test)
    End Sub

    Public Function StripChars(ByVal text As String) As String
    Dim bytVals() As Byte
    Dim bytRtnVal() As Byte
    Dim lngChr As Long
    Dim lngIndx As Long
    If LenB(text) Then
    bytVals = text
    ReDim bytRtnVal(UBound(bytVals))
    For lngChr = 0 To UBound(bytVals) Step 2&
    Select Case bytVals(lngChr)
    Case 34, 42, 47, 58, 60, 62, 63, 92, 124 'Do Nothing
    Case Else
    bytRtnVal(lngIndx) = bytVals(lngChr)
    lngIndx = lngIndx + 2&
    End Select
    Next
    ReDim Preserve bytRtnVal(lngIndx)
    End If
    StripChars = bytRtnVal
    End Function[/VBA]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Quick question: Why the 'Step 2&' and the '+ 2&' ?

    It looks like you're working a byte pairs. Is that necessary for a string?

    Paul

  5. #5
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    In VB and it's many derivatives Strings are in encoded as Unicode. Unicode is two bytes, however it's set up in such a way that for ASCII characters the second byte is always zero. As the characters you want to eliminate are all from the original ASCII set you can just check the first digit. Although if you want to 100% sure you should double check the second digit is a zero to prevent certain non-English characters from being eliminated. I put a quick and dirty way below. You can, of course, restructure if you wish to avoid the evil goto :
    [vba]Public Function StripChars(ByVal text As String) As String
    Dim bytVals() As Byte
    Dim bytRtnVal() As Byte
    Dim lngChr As Long
    Dim lngIndx As Long
    If LenB(text) Then
    bytVals = text
    ReDim bytRtnVal(UBound(bytVals))
    For lngChr = 0 To UBound(bytVals) Step 2&
    Select Case bytVals(lngChr)
    Case 34, 42, 47, 58, 60, 62, 63, 92, 124 'Do Nothing
    If bytVals(lngChr + 1&) <> 0 Then Goto SaveChar
    Case Else
    SaveChar:
    bytRtnVal(lngIndx) = bytVals(lngChr)
    lngIndx = lngIndx + 2&
    End Select
    Next
    ReDim Preserve bytRtnVal(lngIndx)
    End If
    StripChars = bytRtnVal
    End Function [/vba]
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  6. #6
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    or one I can understand!!!
    [VBA]
    Sub Test()
    Dim arr, a
    Dim cel As Range
    arr = Array("\", "/", ":", "*", "?", Chr(34), "<", ">", "|")
    For Each cel In Selection
    For Each a In arr
    cel.Value = Application.Substitute(cel, a, "")
    Next
    Next
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location

    Talking

    Mine runs faster

    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    I'm sure it does. I'll work on it!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Tutor Benzadeus's Avatar
    Joined
    Dec 2008
    Location
    Belo Horizonte, Brazil
    Posts
    271
    Location
    Aaron,

    Quick question:

    Is there a difference using 2 or 2& or it is just esthetics?

    I mean... does the virtual machine that execute VBA code identify faster that 2& is a long than using just 2?

  10. #10
    Knowledge Base Approver VBAX Master Oorang's Avatar
    Joined
    Jan 2007
    Posts
    1,135
    Location
    Honestly... I really doubt it makes any measurable difference at all Technically it prevents a type conversion. 2 is an integer 2& is a long. Maybe if you ran the function a couple million times in a row you would can a few milliseconds. I suppose I've gotten a bit anal in my code. Probably comes from writing UDFs for database(s) where a function really could be called a million times in a query, micro-optimization is more important under those circumstances. But for most purposes it will make little to no noticable difference. (For that matter, you probably wouldn't even notice the difference between mine and Malcoms unless you were using them A.) In a UDF and B.) Using that UDF tens of thousands of times).

    I just like to hassle Malcolm cause he's got broad shoulders.

    But as a habit I usually try to type a literal to the same type as the variable it's being used with. It's not always possible though. Notice that there is no type declaration character for Byte so I left that case alone.
    Cordially,
    Aaron



    Keep Our Board Clean!
    • Please Mark your thread "Solved" if you get an acceptable response (under thread tools).
    • Enclose your code in VBA tags then it will be formatted as per the VBIDE to improve readability.

Posting Permissions

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