Hello Everyone!
I've written this 'Keywords_Extract' Function (The function process a string, separates the words and returns the results to and undefined array.) many times and have finally come up with a version that seems to be stable and preform exactly how I need it to. However, I'm concerned about the required ErrorHandler: that I have written to make the code execute properly.
Once the code reaches the statement:
ReDim Preserve Keyword_List(UBound(Keyword_List) + 1) As String
the code returns a subscript out of range error due to passing the undefined array to the Ubound Function. I initially expected this to return False or 0 but this does not seem to be the case. To correct the problem I have written in some Error Handling that assumes on Error 9 the Keyword_List array has not yet been defined and defines it. Though this works, I'm concerned about making an assumption.
Is there any way return the value 0 (zero) from an undefined array or check if Error Code 9 was result of the specified undefined array?
I want to eliminate the ErrorHandler as a requirment or modify it so it's not assuming the error was caused by the undefined array. I do realize that I could probably define the array to 1 after the initial Dim statement and it may produce the desired results however, it doesn't feel like the best solution. (I could be wrong though.).
Any suggestion, comments, or ideas would be appreciated.
Thanks in advanced.
Option Explicit
Option Base 1
Function KeyWords_Extract(ByVal InpString As String) As Variant
On Error GoTo ErrorHandler
Dim Keyword_List() As String
Dim wBuffer As String
Do While InStr(1, InpString, Chr(32)) > 0
DoEvents
InpString = mRtrim(mLtrim(InpString))
Select Case InStr(1, InpString, Chr(32))
Case Is > 0
ReDim Preserve Keyword_List(UBound(Keyword_List) + 1) As String
wBuffer = Mid(InpString, 1, InStr(1, InpString, Chr(32)) - 1)
Keyword_List(UBound(Keyword_List)) = wBuffer
InpString = Mid(InpString, InStr(1, InpString, Chr(32)), (Len(InpString) - InStr(1, InpString, Chr(32))) + 1)
Case Is = 0
ReDim Preserve Keyword_List(UBound(Keyword_List) + 1) As String
wBuffer = InpString
Keyword_List(UBound(Keyword_List)) = wBuffer
InpString = InpString
End Select
Loop
KeyWords_Extract = Keyword_List()
Exit Function
ErrorHandler:
Select Case Err.Number
Case 9
ReDim Keyword_List(1) As String
Resume Next
Case Else
On Error GoTo 0
End Select
End Function
The following 2 functions are used to remove multiple spaces from the beginning and end of a string.
Function mRtrim(ByRef InpString As String) As String
Do While Mid(InpString, Len(InpString), 1) = Chr(32)
DoEvents
InpString = Mid(InpString, 1, Len(InpString) - 1)
Loop
mRtrim = InpString
End Function
Function mLtrim(ByRef InpString As String) As String
Do While Mid(InpString, 1, 1) = Chr(32)
DoEvents
InpString = Mid(InpString, 2, Len(InpString) - 1)
Loop
mLtrim = InpString
End Function
Bookmarks