Logo 
Search:

MS Office Answers

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds
  Question Asked By: Kenneth Bennett   on Jan 11 In MS Office Category.

  
Question Answered By: Zakary Thompson   on Jan 11

Well, there are a couple things happening to you. First, you're using
mathematical operators on Strings which is likely to force VB to push the
variables to a different type for evaluation *if* you don't get a type
mismatch error. This is commonly referred to Evil Type Coercion. Kinda ugly
and full of unpredictable results. Second, the expect result of a function
is a result but you didn't get around to assigning a result to your
function.

Here's a linear subroutine which does what you asked for:
Sub Checkval()

Dim FinalVal As Long
Dim FirstVal As Long
Dim SecondVal As Long
Dim ThirdVal As Long
Dim FourthVal As Long

FirstVal = 20000
SecondVal = 9
ThirdVal = -6
FourthVal = 0

'Set the initial value of FinalVal
'to the same as FirstVal
FinalVal = FirstVal

If SecondVal < FinalVal Then
FinalVal = SecondVal
MsgBox _
"SecondVal was less than FinalVal: " & _
FinalVal
End If

If ThirdVal < FinalVal Then
FinalVal = ThirdVal
MsgBox _
"ThirdVal was less than FinalVal: " & _
FinalVal
End If

'This one will be skipped because
'zero is greater than -6
If FourthVal < FinalVal Then
FinalVal = FourthVal
MsgBox _
"FourthVal was less than FinalVal: " & _
FinalVal
End If

MsgBox "FinalVal: " & FinalVal
End Sub

Were you to turn this into a routine to do constant comparisons, you'd need
something to treat like a global variable  and then pass your new values to
the routine and update the global variable only if the condition of 'less
than' is true.
'============================================
Public Finalval as Long
'============================================
Sub CompareVals()
Dim lng1 as Long
Dim lng2 as Long
Dim lng3 as long

FinalVal=40000000
Lng1=20
Lng2=-6
Lng3=0

FinalVal=CheckVal(Lng1)
FinalVal=CheckVal(Lng2)
FinalVal=CheckVal(Lng3)

MsgBox FinalVal
End Sub
'============================================
Function CheckVal(lngVal as Long) As Long

If LngVal < FinalVal Then
CheckVal=LngVal
Else
Checkval=FinalVal
End If
'Debug.Print shows output in the Immediate
'Window in the VBE
Debug.Print CheckVal
End Function

Share: 

 

This Question has 13 more answer(s). View Complete Question Thread

 
Didn't find what you were looking for? Find more on Help with Value in a Variable Or get search suggestion and latest updates.


Tagged: