IF AND in VBA

  • Thread starter Thread starter ExcelMonkey
  • Start date Start date
E

ExcelMonkey

I use "IF And" statments in excel alot. But in VBA I dont think you
don't use the brackets with "IF AND". How would your write the
following in VBA?



=IF(AND(RAND()<0.5,RAND()>0.1,),"Yes",IF(AND(RAND()>0.5,RAND()<0.9,),"Yahoo","No"))

Note you will have to replace the excel rand() function with the VBA
rnd() function.
 
Try something like

Dim R As Double
R = Rnd()
If R < 0.5 And R > 0.1 Then
Debug.Print "Yes"
Else
If R > 0.5 And R < 0.9 Then
Debug.Print "Yahoo"
Else
Debug.Print "No"
End If
End If



--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
E-M

myVar = Rnd
If myVar <0.5 And myVar > .1 Then
MsgBox "Yes"
ElseIf myVar > .5 And Rnd < .9 Then
MsgBox "Yahoo"
Else
MsgBox "No"
End If

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)
 
You realize that in your worksheet function, you are getting 4 different
random numbers - this fact wasn't duplicated by Chip or Bob.
 

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