Formula in macro help

  • Thread starter Thread starter Biff
  • Start date Start date
B

Biff

Hi Folks!

I use this formula extensively for testing:

=ROUND(RAND()*100,0)

That simply generates random 2 digit integers.

What I would like is a macro that will insert that formula into the selected
range then do the equivalent of Copy>Paste Special>Values.

It doesn't necessarily have to "insert that formula" just so the macro
generates random 2 digit ints in the selected range.

I use this so often it makes sense to have a macro that I can attach to a
toolbar button.

Thanks!

Biff
 
One way, if you have the range selected:

Sub Test()
With Selection
.Formula = "=ROUND(RAND()*100,0)"
.Copy
.PasteSpecial xlValues
End With
'Application.CutCopyMode = False
End Sub

Or you could just use Range("A1:A2") or equivalent insead of Selection. This
would be better as there would be no need then to select the range.

Regards,

Vasant
 
Biff,

Sub test()
Selection.Formula = "=ROUND(RAND()*100,0)"
Selection.Value = Selection.Value
End Sub

hth,

Doug
 
Thanks Vasant. Perfect!

Biff

Vasant Nanavati said:
One way, if you have the range selected:

Sub Test()
With Selection
.Formula = "=ROUND(RAND()*100,0)"
.Copy
.PasteSpecial xlValues
End With
'Application.CutCopyMode = False
End Sub

Or you could just use Range("A1:A2") or equivalent insead of Selection.
This would be better as there would be no need then to select the range.

Regards,

Vasant
 
Thanks Doug.

Man, that seemed too easy! I gotta get up to speed with VBA one of these
days.

Biff
 
Oops. That won't work. My mistake. You said a range. Didn't notice till
after hitting send. Sorry.

--
Dana


Dana DeLouis said:
Just another option:

Sub Demo()
ActiveCell = [ROUND(RAND()*100,0)]
End Sub

HTH :>)
--
Dana DeLouis
Win XP & Office 2003


Biff said:
Thanks Doug.

Man, that seemed too easy! I gotta get up to speed with VBA one of these
days.

Biff
 
Dana,

I tried something like that- are the brackets the same as Evaluate?
Anyways, since Biff wants to fill a range, I don't think thatworks - at
least when I change Activecell to Selection I get the same "random" number
in each cell.

Doug

Dana DeLouis said:
Just another option:

Sub Demo()
ActiveCell = [ROUND(RAND()*100,0)]
End Sub

HTH :>)
--
Dana DeLouis
Win XP & Office 2003


Biff said:
Thanks Doug.

Man, that seemed too easy! I gotta get up to speed with VBA one of these
days.

Biff
 

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