Using Variables and Constants

shivahsingh

New Member
Joined
Oct 1, 2014
Messages
10
This assignment requires you to create 2 macros in one module. Create one global constant and one global variable as follows which are to be used in both macros.
a constant named as “tax” of type double to store tax percentage
a variable named as “totalSales” of type currency
Part 1
You’re to write a macro named as “calculateTotalSales1” to calculate the total sales of four items in your shopping basket. You’ll need to create only one variable named as price of type currency which will be local to this macro. You’re asked to use the variable price four times to get the price for each of the four items in the grocery basket, and make sure you’re adding the price of an item to the totalSales before ask user to enter the price for the next item. Eventually you will be calculating the following:
totalsales= sumofprices+tax/100*sumofprices

I've declared my variables and constant, still confused on how to set the constant as a percentage

and how to use the price variable to create a function,
how do i refer to the cells as prices
please help this is my first class and first time dealing with vba

the sales numbers are in cells e2:e5
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
A very helpful source of information is the help file. It even has examples. Why not just ask the teacher?


Const Statement

Declares constants for use in place of literal values.

Syntax

[Public | Private] Const constname [As type] = expression

The Const statement syntax has these parts:

Part Description
Public Optional. Keyword used at module level to declare constants that are available to all procedures in all modules. Not allowed in procedures.
Private Optional. Keyword used at module level to declare constants that are available only within the module where the declaration is made. Not allowed in procedures.
constname Required. Name of the constant; follows standard variable naming conventions.
type Optional. Data type of the constant; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, or Variant. Use a separate As type clause for each constant being declared.
expression Required. Literal, other constant, or any combination that includes all arithmetic or logical operators except Is.

Remarks

Constants are private by default. Within procedures, constants are always private; their visibility can't be changed. In standard modules, the default visibility of module-level constants can be changed using the Public keyword. In class modules, however, constants can only be private and their visibility can't be changed using the Public keyword.

To combine several constant declarations on the same line, separate each constant assignment with a comma. When constant declarations are combined in this way, the Public or Private keyword, if used, applies to all of them.

You can't use variables, user-defined functions, or intrinsic Visual Basic functions (such as Chr) in expressions assigned to constants.


Note
Constants can make your programs self-documenting and easy to modify. Unlike variables, constants can't be inadvertently changed while your program is running.


If you don't explicitly declare the constant type using As type, the constant has the data type that is most appropriate for expression.

Constants declared in a Sub, Function, or Property procedure are local to that procedure. A constant declared outside a procedure is defined throughout the module in which it is declared. You can use constants anywhere you can use an expression.

Example
This example uses the Const statement to declare constants for use in place of literal values. Public constants are declared in the General section of a standard module, rather than a class module. Private constants are declared in the General section of any type of module.

' Constants are Private by default.
Const MyVar = 459

' Declare Public constant.
Public Const MyString = "HELP"

' Declare Private Integer constant.
Private Const MyInt As Integer = 5

' Declare multiple constants on same line.
Const MyStr = "Hello", MyDouble As Double = 3.4567
 
Upvote 0

Forum statistics

Threads
1,213,533
Messages
6,114,179
Members
448,554
Latest member
Gleisner2

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