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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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