Problem

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134
Hi,
I am writing a small program in VB.It requires the user to input 3 numbers,the program will then show the largest of these.
I know this isnt a VB forum, but I think an IF statement could produce what I am looking for.
I have tried several solutions but cannot come up with one that will do the business.
Anyone have any ideas?
Thanks
Charlie
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

Jack in the UK

Well-known Member
Joined
Feb 16, 2002
Messages
3,215
This is in VBA Excel but should help its as simple as i can make it so you can amend to VB if she trips - i never use VB sorry

Try this tested works fine in excel
run on command button and askes 3 numbers and message box gives max number input that round as you requested

Check your PM also
Sub CharlieUK()
Dim One As Variant
Dim Two As Variant
Dim Three As Variant
Dim AOne As Variant
Dim ATwo As Variant
Dim AThree As Variant
Dim JacksAns As Variant


One = Application.InputBox("Enter a number")
Two = Application.InputBox("Enter a number")
Three = Application.InputBox("Enter a number")
JacksAns = WorksheetFunction.Max(One, Two, Three)
Response = MsgBox(JacksAns)

End Sub
 

Jack in the UK

Well-known Member
Joined
Feb 16, 2002
Messages
3,215
OR shorter

Sub CharlieUK()
Dim One, Two, Three As Variant
Dim JacksAns
One = Application.InputBox("Enter a number")
Two = Application.InputBox("Enter a number")
Three = Application.InputBox("Enter a number")
JacksAns = WorksheetFunction.Max(One, Two, Three)
Response = MsgBox(JacksAns)
End Sub
 
L

Legacy 98055

Guest
Hi Charlie.
I am a VB programmer and have found this site to be excellent with very few of my questions going unanswered. If you only expect to deal with several numbers as opposed to many, create a simple function and call it from your procedure. I'll just assume you are using individual textboxes for your user input. Provide more details up front when posting to a board. As you go on, you may want to take the time to learn how to store all of your functions in a dll file. For now, a separate module or even just notepad will do.

This project includes:
text1
text2
text3
Command1
Label1

<pre>
Private Sub Command1_Click()

'validate your users input
If Not IsNumeric(Text1) Then
MsgBox "Please enter a numeric value."
Text1.SetFocus: Exit Sub
ElseIf Not IsNumeric(Text2) Then
MsgBox "Please enter a numeric value."
Text2.SetFocus: Exit Sub
ElseIf Not IsNumeric(Text3) Then
MsgBox "Please enter a numeric value."
Text3.SetFocus: Exit Sub
End If

Label1 = MaxVal(Text1, Text2, Text3)
End Sub

Private Function MaxVal(ParamArray vals()) As Double
Dim ValCntr As Long, CheckVal As Double
If IsMissing(vals) Then
MaxVal = -1 'error return value
Exit Function
End If
CheckVal = vals(0)
For ValCntr = 0 To UBound(vals)
If Val(vals(ValCntr)) > CheckVal Then _
CheckVal = Val(vals(ValCntr))
Next
MaxVal = CheckVal
End Function

</pre>
Tom
 

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134

ADVERTISEMENT

Thanks All,
This is the code I have written so far.

Private Sub cmdStart_Click()
'Declare storage space
Dim Variable1 As Integer
Dim Variable2 As Integer
Dim Variable3 As Integer
Dim Variable4 As Integer

'Invite user to input numbers
Variable1 = Val(InputBox("Please enter numeber ", "INTEGER"))
Variable2 = Val(InputBox("Please enter second number ", "INTEGER"))
Variable3 = Val(InputBox("Please enter third number ", "INTEGER"))
'Message in picture box
picMessage.Print " The user has entered "; (Variable1)
picMessage.Print "The user has enterd "; (Variable2)
picMessage.Print "The user has entered "; (Variable3)
End Sub

What I was thinking of using is.
Variable4 = If(Var1 > Var2)Or (Var3< Var2)Then....something like that.
So I can then use, picMessage.Print "The highest value number entered was "(Variable4)
Unfortunately I cant get this one to work, but its along these lines the answer to my problem lies.
Your solution Tom looks great unfortunately im not an advanced user so I would like to keep my code as simple as possible for the sake of me remembering it.
Thanks again for answering, this site is a great help as always.
Charlie
This message was edited by Charlie on 2002-10-11 02:36
 
L

Legacy 98055

Guest
Charlie.
Not advanced here. I'm a truck driver who does this on the side for extra cash. If you do not want to learn, please note that in your original post.<pre>
Private Sub cmdStart_Click()
'Declare storage space
Dim MyVal(2) As Integer

'Invite user to input numbers
MyVal(0) = Val(InputBox("Please enter number ", "INTEGER"))
MyVal(1) = Val(InputBox("Please enter second number ", "INTEGER"))
MyVal(2) = Val(InputBox("Please enter third number ", "INTEGER"))

'Message in picture box
picMessage.Print " The user has entered "; MyVal(0)
picMessage.Print "The user has enterd "; MyVal(1)
picMessage.Print "The user has entered "; MyVal(2)
picMessage.Print "The largest number entered is "; MaxVal(MyVal(0), MyVal(1), MyVal(2))
End Sub

Private Function MaxVal(ParamArray vals()) As Double
Dim ValCntr As Long, CheckVal As Double

CheckVal = vals(0)

For ValCntr = 0 To UBound(vals)
If Val(vals(ValCntr)) > CheckVal Then _
CheckVal = Val(vals(ValCntr))
Next

MaxVal = CheckVal

End Function</pre>

Re-usable code is the goal with object based programming and
you should spend the 15 or 20 minutes it would take to learn
to create a basic function. A function is pretty much the
same as a "Sub" except that it can return a value. You are
simply passing your arguments to the function, it does it's
thing, and returns the results. For more help, highlight
"Function", "ParamArray", "Ubound", ect... and press F1.

Tom
This message was edited by TsTom on 2002-10-11 06:14
 

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134
Thanx for the reply Tom,

In answer to your post.I do want to learn but seeing as I have only been doing this VB for a couple of days I tend to look for the simplest solutions.
Your solutions may look simple to you but I can assure you that they do not to the novice.
I have however came up with a "Simpler" solution:

'Compare first and second numbers - store largest
If Num1 > Num2 Then
Largest = Num1
Else
Largest = Num2
End If

'Compare largest of first and second numbers with third - output largest
If Largest > Num3 Then
picOutput.Print "The largest number entered was " & Largest
Else
picOutput.Print "The largest number entered was " & Num3
End If

What do you reckon then Tom,am I talking sense or.
Charlie
This message was edited by Charlie on 2002-10-11 08:31
 

Forum statistics

Threads
1,144,221
Messages
5,723,115
Members
422,478
Latest member
Rovan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top