ChrisGreaves wrote: ↑21 Jan 2024, 12:33
I shall post my numeric-testing VBA code after I have cleared my emails.
As promised:-
Code: Select all
Public Function strOnly(strIn As String, strRef As String) As String
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Function: strOnly
'''
''' Comments: Returns only those characters in strIn which can be found in strRef.
'''
''' Arguments: STRING String to be examined.
''' STRING Set of characters thought to exist in the first string.
'''
''' Returns: STRING Those characters, in sequence, in strIn which belong to strRef
'''
''' Date Developer Action
''' --------------------------------------------------------------------------
''' 2008/11/22 Chris Greaves Created
'''
Dim strOut As String
strOut = ""
Dim lngI As Long
For lngI = 1 To Len(strIn)
Dim strMid As String
strMid = Mid(strIn, lngI, 1)
If InStr(1, strRef, strMid) > 0 Then
strOut = strOut & strMid
Else
End If
Next lngI
strOnly = strOut
'Sub TESTstrOnly()
' Debug.Assert "aabe" = strOnly("alphabet", "abcde") ' regular use "aabe" of 1st string exist in 2nd string
' Debug.Assert 0 = Len(strOnly("", "abcde")) ' empty source string yields empty result string, length = 0
' Debug.Assert 0 = Len(strOnly("alphabet", "")) ' empty reference string yields empty result string, length = 0
' Debug.Assert "alphabetanddigit" = strOnly("alphabetand1digit", "abcdefghijklmnopqrstuvwxyz") ' strip digit "1"
'End Sub
End Function
This is my basic "slave" or low-level function which is used in a variety of other functions.
The reference string can be any string, but some basic reference strings are provided below.
This function is a self-testing function: Drag the commented "Sub Test" lines outside the function and run the macro "TESTstrOnly"; the macro should run with no visible output. If there is a problem then the macro will halt on the appropriate "Debug.Assert" command.
Auxiliary constants
Code: Select all
Public Const strcLowerAlpha As String = "abcdefghijklmnopqrstuvwxyz"
Public Const strcUpperAlpha As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Public Const strcAlpha As String = strcLowerAlpha & strcUpperAlpha
Public Const strcDigits As String = "0123456789"
Public Const strcAlphaDigits As String = strcAlpha & strcDigits
'
'''
''' Neman's definition of illegal characters in a path item
'''
Public Const strcTypeableIllegalCharacters As String = "\/:*?""<>|" ' NS:348
Public Const strcTypeableLegalCharacters As String = "`~!@#$%^&()-_+=}]{[';,"
Public Const strcTypeableCharacters As String = " " & "." & strcTypeableIllegalCharacters & strcTypeableLegalCharacters
Public Const strcTLC As String = strcAlphaDigits & strcTypeableLegalCharacters
'''
Cheers, Chris