PDA

View Full Version : [SOLVED] No rush, just to improve myself



ValerieT
07-28-2014, 04:54 AM
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

ranman256
07-28-2014, 05:21 AM
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

westconn1
07-28-2014, 05:28 AM
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

ValerieT
07-28-2014, 06:18 AM
thanks, working fine.