Consulting

Results 1 to 4 of 4

Thread: No rush, just to improve myself

  1. #1
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location

    No rush, just to improve myself

    Hello

    I am learning to create function. As you can see, the 2 below are identical twins. I am sure I could use an optional parameter to have 1 function only? (example: Optional parameter "M" would return the GetMissCol part, Optional parameter "L" would return the GetLastCol part)

    but I can't make it work

    Public Function GetMissCol()
    GetCol
    Dim MyFind As String
        MyRes = 0
        list = Split(AllConst, ";")
        For i = 0 To UBound(list)
        MyFind = list(i)
            If GetConst(MyFind) = 0 Then
            MyRes = MyRes & MyFind & "; "
            End If
        Next i
        If MyRes = 0 Then
           MyRes = "Everything is OK"
           End If
        GetMissCol = MyRes
    End Function
    
    Public Function GetLastCol()
    GetCol
    Dim MyFind As String
        MyRes = 0
        list = Split(AllConst, ";")
        For i = 0 To UBound(list)
        MyFind = list(i)
            If GetConst(MyFind) > MyRes Then
               MyRes = GetConst(MyFind)
            End If
        Next i
        GetLastCol = MyRes
    End Function

  2. #2
    VBAX Tutor
    Joined
    Mar 2014
    Posts
    210
    Location
    So you would have 3 options for optional:
    M, L, or nothing (hence the optional , meaning you dont have to give one)
    'as it looks...you dont have an option...just 2 possiblities ,M or L. But ammend code as you need.

     
    Public Function GetCol(optional pvArg) 
    If isMissing(pvArg) Then pvArg = ""   'just to prevent errors
    Select Case pvArg 
      Case "L" 
         'code for L option
      Case "M" 
         'code for M option
      Case Else 
         'code for no option
    End Select

  3. #3
    you could try like
    Public Function GetCol(Optional s As String = "M") 
        GetCol 
        Dim MyFind As String 
        MyRes = 0 
        list = Split(AllConst, ";") 
      select case s
        case "M"
          For i = 0 To UBound(list) 
              MyFind = list(i) 
              If GetConst(MyFind) = 0 Then 
                  MyRes = MyRes & MyFind & "; " 
              End If 
          Next i 
          If MyRes = 0 Then 
            MyRes = "Everything is OK" 
          End If 
        case "L"
          For i = 0 To UBound(list) 
              MyFind = list(i) 
              If GetConst(MyFind) > MyRes Then 
                  MyRes = GetConst(MyFind) 
              End If 
          Next i 
      end select
        GetCol = MyRes 
    End Function

  4. #4
    VBAX Regular
    Joined
    Mar 2013
    Posts
    80
    Location
    thanks, working fine.

Posting Permissions

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