beginner to VBA: adding formula on Visual Basic

Fmmleohb

New Member
Joined
Sep 27, 2022
Messages
1
Office Version
  1. 2003 or older
Platform
  1. Windows
Dear all,

I am a beginner to VB and i trust this forum can help me to sharpen my skills.

how do I add the simple calculations on VBA instead of the spread sheet. ie i have an excel sheet with data that has formulas, i want these formulas to disappear in excel however it should only appear in MACROS. formulas such as (multiply, % change , add , subtract, avg, divide)

can you kindly explain with an example ?
much appreciated
regards,
leo
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Let's say you have numbers in A1 and B1. In C1 you could use something like "=AVERAGE(A1:B1)" to get an average of the two cells.

In VBA, you could create a macro to do this, or write your own UDF (User-Defined Function). For code that calculates and returns results, it's often sufficient to use a UDF. For code to DO things, like change layouts/rows/columns, etc., a macro is used. (Macros can also call Functions to handle calculations and return values to the macro.)

To create a UDF, in the VBA Editor (ALT+F11), click Insert > Module. In that module add the following code per our earlier example of averaging a range of cells:
VBA Code:
Public Function myAvg(r As Range)
myAvg = WorksheetFunction.Average(r)
End Function

Then in C1, you could use the formula:
Excel Formula:
=myAvg(A1:B1)

This is obviously a simple example - it can get much more complex, of course.
 
Upvote 0
I am trying to understand your need. Do you want just values in the worksheet without formulas? So a macro would go through the formulas and replace them with the calculated value?
 
Upvote 0

Forum statistics

Threads
1,215,771
Messages
6,126,799
Members
449,337
Latest member
BBV123

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