Max of integer vals

  • Thread starter Thread starter jbl25
  • Start date Start date
J

jbl25

Hello,
How do I take the max of 2 integer values in VBA? (I'd like to set
variable = to the max of itself and a cell reference which contains a
int) Thank you
 
One way:

Sub test()
' put a value in cell B3, say 10
Dim i As Long
i = 3
i = WorksheetFunction.Max(i, Range("B3"))
MsgBox i
End Sub

Regards

Trevor
 
If you only do 2
Dim intVal as integer, intMax as Integer
intVal = int(rnd() * 2 * Range("B9").Value + 1)
if intVal > Range("B9").value then
intMax = intValue
Else
intMax = Range("B9").Value
End if

or use the immediate IF
intMax = iif(intVal>Range("B9"),intVal,Range("B9"))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top