calling excel function inside vba code

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi I am trying to learn how to call excel function inside a vba code but I got an error message doing the following

Sub fillme()
Range("a1:c30") = randbtween(1, 100)
End Sub


any help please. Thank you so much
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
You need to use the WorksheetFunction.

Code:
Dim oneCell as Range

For Each oneCell in Range("A1:C30")
    oneCell.Value = WorksheetFunction.RandBetween(1, 100)
Next oneCell

Or you could do it in bulk

Code:
With Range("B1:C30")
    .Formula = "=RANDBETWEEN(1, 100)"
    .Value = .Value
End With
 
Last edited:
Upvote 0
Use Application.Worksheetfunction.Randbetween and not just Randbetween (and certainly not Randbtween! ;)).
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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
Back
Top