Gillian has attended:
Access VBA course
Excel Advanced course
Ascertaining a minimum value
Hello,
Is there a way to take two values, and identify which is the minimum?
I have two text fields, and for each row, I want the length value of the field that has the smallest length string.
E.g. for 'lion' and 'giraffe', I would like the value of 4 to be returned, the length of the smallest word.
Thankyou
RE: Ascertaining a minimum value
Hi Gillian
Thank you for your question
The following code will solve your problem. It prompts the user for two words and uses the len function to determine the length of the words. It then returns the shortest word, or indicates that they are of the same length.
you can adapt this by setting the variables equal to your field values
Sub StringLengthComp()
Dim strFirst As String
Dim strSecond As String
strFirst = InputBox("Enter First Word")
strSecond = InputBox("Enter second Word")
If Len(strFirst) = Len(strSecond) Then
MsgBox "Both words are the same length"
Exit Sub
End If
If Len(strFirst) > Len(strSecond) Then
MsgBox strSecond
Else
MsgBox strSecond
End If
Regards
Stephen