calling function in excel vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I noticed that i can not right sum() function alone like left(). what i mean by that, i have to use this syntax to call sun() function

x = Excel.WorksheetFunction.Sum(Range("a11:a20"))

however I can use left() function without using Excel.worksheetfucntion like the following

Cells(20, 3) = Left(Range("a1"), 3)

I tried to use sum() like the following

x = Sum(Range("a11:a20"))

but I got compile error msg - sub or function is not defined. Thanks
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Some functions, like LEFT() are both Excel functions and VBA functions.

Others, like Excel's SUM(), have no VBA equivalent, but can be called from VBA using WorksheetFunction.Sum or Application.Sum

If you look at the Members of the WorksheetFunction Object, you'll see that many, but not all, Excel functions can be called from VBA.
For example, you can't call Excel's MAX or MIN functions.

Also, be careful, there are differences in the way some functions behave between Excel and VBA. Even though VBA has a Round function, I will often use Application.Round so that my results round the same way that Excel does, rather than using VBA's "Banker's rounding".
 
Upvote 0
You have to use the WorksheetFunction or Application for functions that are not native to VBA. The SUM function is not native to VBA, therefore you have to tell VBA that that procedure is found in the application.

Here's a good description of Worksheet functions.
The WorksheetFunction Method – Daily Dose of Excel
 
Upvote 0

Forum statistics

Threads
1,214,426
Messages
6,119,411
Members
448,894
Latest member
spenstar

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