Results 1 to 14 of 14

Initial Declaration of Non-Static Array in Function

Threaded View

  1. #1
    Registered User
    Join Date
    05-03-2011
    Location
    Salt Lake City
    MS-Off Ver
    Excel 2003
    Posts
    15

    Exclamation Initial Declaration of Non-Static Array in Function

    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
    Last edited by Extradimensional; 09-10-2011 at 12:52 AM. Reason: Solved

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1