Vba excel with currency

VBA Currency Data Type in Excel explained with syntax and examples. We use the Currency VBA data type to store numbers in integer format and these numbers scaled by 10,000. The default value is 0. The Currency VBA data type contains range of values between -922,337,203,685,477.5808 to 922,337,203,685,477.5807. The data type Currency can be used store both positive and negative numbers. It is one of the VBA data type and occupies 8 bytes(64 bits) in memory.

Note: The currency VBA data type is useful for money, monetary or fixed-point calculations. It has type declaration character. It is nothing but character appended to a variable instead of data type. The type declaration character for currency VBA data type is the explanation point(@).

Table of Contents:

  • Objective
  • Data Type Currency Syntax in Excel VBA
  • Example on the Currency Data Type in Excel VBA
  • Convert an Expression to Currency VBA Data Type
  • Instructions to Run VBA Macro Code
  • Other Useful Resources

Let us see Data Type Currency Syntax in Excel VBA.

Dim VariableName as Currency

Where VariableName represents the name of the variable.
and Currency represents the type of data.

Example on the Currency VBA Data Type in Excel

Here is an example on the Data Type Currency in Excel VBA.

Sub VBAF1_Currency_DataType2()
    
    'Integer Variable Declaration
    Dim cValue As Currency
    
    'Assign value to currency variable
    cValue = 888866664444.2222@
        
End Sub

Convert an Expression to Currency Data Type in Excel VBA

You can convert an expression to Currency VBA data type using VBA CCur function. Click on the following link to learn about CCur function and complete tutorial and examples.

VBA CCur Function

VBA Data Types

Also read about all other data types.

VBA Data Types

Instructions to Run VBA Macro Code or Procedure:

You can refer the following link for the step by step instructions.

Instructions to run VBA Macro Code

Other Useful Resources:

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables

VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog

Let’s take an example to understand how and where we can format the cell in currency.

We have product’s amount data in range A1:B9 in which column A contains Product name and column B contains amount of it. We want to format the amount range as currency so that, for user, it should be easy to understand that a number is a currency.
img1

To write the macro, follow below given steps:-

  • Open VBA Page and press the key Alt+F11.
  • Insert a module.
  • Write the below mentioned code:

Sub FormatCurrency()

Dim Sh As Worksheet

Set Sh = ThisWorkbook.Sheets(1)

Sh.Range(«B2:B9»).NumberFormat = «$#,##0.00»

End Sub

Code Explanation:-

First, we have given the subject for the code, then we had defined the all variables after that we had defined the range where we want to put the currency format.

To run the macro, press the key F5, and the range format will be changed into the currency format.

img2

@ www.excelforum one of the users has asked the similar question:-

Currency Format, written in US run in Shanghai

I have an access db that creates / populates Excel and then formats the report. I have currency that is in USD so I format it as when I run in the US, I get what I expect «$100,000», but when this application is run in Shanghai, they get and of course, the actual values are the same if run in US of Shanghai.
How so I get it to show USD when run from Shanghai?

To check the solution please click here

http://www.excelforum.com/excel-programming-vba-macros/1075253-currency-format-written-in-us-run-in-shanghai.html

xlsx-1567

Download — Formatting Currency in VBA — xlsm

Home / VBA / Top VBA Functions / VBA FORMATCURRENCY Function (Syntax + Example)

The VBA FORMATCURRENCY function is listed under the data type conversion category of VBA functions. When you use it in a VBA code, it returns a string by applying currency format to a number. In simple words, you can convert a number into a value in which currency format is applied.

FormatCurrency(Expression,[NumDigitsAfterDecimal],[IncludeLeadingDigit],[UseParensForNegativeNumbers],[GroupDigits])

Arguments

  • Expression: The numeric value which you want to format as currency.
  • [NumDigitsAfterDecimal]: A numeric value to specify the decimals [This is an optional argument and if omitted -1 by default].
  • [IncludeLeadingDigit]: An enumeration value to specify whether a leading zero should be displayed [This is an optional argument and if omitted vbUseDefault by default].
    • vbFalse: To not to a leading zero.
    • vbTrue: Display a leading zero.
    • vbUseDefault: Use the default settings.
  • [UseParensForNegativeNumbers]: An enumeration value to specify whether negative numbers should be encased in parentheses [This is an optional argument and if omitted vbUseDefault by default].
    • vbFalse: To not encase negative numbers in parentheses.
    • vbTrue: Encase negative numbers in parentheses.
    • vbUseDefault: Use the default settings.
  • [GroupDigits]: n enumeration value to specify whether the number should be grouped (into thousands, etc.), using the group delimiter that is specified on the computer’s regional settings [This is an optional argument, and if omitted vbUseDefault by default].
    • vbFalse: To not use group digits.
    • vbTrue: To use group digits.
    • vbUseDefault: Use the default computer settings.

Example

To practically understand how to use VBA FORMATCURRENCY function, you need to go through the below example where we have written a vba code by using it:

Sub example_FORMATCURRENCY()
Range("B1").Value = FormatCurrency(Range("A1"))
End Sub

In the above code, we have used FORMATCURRENCY to convert the value in cell A1 into a currency value and it has returned it in cell B1 with the dollar sign.

Notes

  • The currency symbol used depends on system settings.
  • If the value specified is a value other than a number or a string that can’t be recognized as a number, VBA will return the run-time 13 error.

Excel VBA Tutorial 5 - Text Box Properties, Text Box Currency Format and Date Format

In Excel VBA tutorial 4 we learned how to add different tools to the user form. In today’s tutorials, we will learn about the properties of text boxes. We will learn about different formats of text boxes.

About User Form Properties In Tutorial 3, we saw some important points. In the same way every tool has its properties. Some tabs are more or less according to the tool.

Excel VBA Tutorial 5 - Text Box Properties, Text Box Currency Format and Date Format

1. Name – Any name can be given to the text box.

2. Text box Size – Option of drag and drop with the mouse is better to increase the size or more.

3. Control Tip Text – When the application is run, you have to type the text in the control tip text to show a text tip as soon as the mouse cursor is moved over the text box.

Excel VBA Tutorial 5 - Text Box Properties, Text Box Currency Format and Date Format

4. Enable – Many times the project has to show the text in the text box, but to ban it from editing, Select False Option in the option of enable, no user can edit the text of the text box.

5. Visible – To hide the text box, the text box will be hidden as soon as it is false in front of the Visible option.

6. Text – To show any text by default in the text box, you can type the words type in front of the text option.

7. With the option of Text Align, Back Color, Border Color, you can change the design of the text box.

8. Password Character – Type one of the special characters in the password char to show the text * or # or @ of the text box as such.

For example – When typing a text box password, all we need to do is to show * star *, type password Char  *  .

Excel VBA Tutorial 5 - Text Box Properties, Text Box Currency Format and Date Format

How to change the text box format?

1. Text box currency format

To change the text box Value to currency format, double click on the text box or right click to view code.

Private Sub TextBox1_Change() 

End Sub

The text box name and change will appear. That means, while making any change in the text box, we can give the command in it. You can see multiple options in the drop down option by clicking above the change option above. We will be able to make any changes according to the behavior of the text box. We have to type the code of the format in the line below Private Sub TextBox1_Change ().

Excel VBA Tutorial 5 - Text Box Properties, Text Box Currency Format and Date Format

Private Sub TextBox1_Change()

Me.TextBox1.Text = Format(Me.TextBox1, “####,##”)

End Sub

Now run the project by pressing F5. And type any number in the text box. The value will be converted to currency format only while typing.

If you need a decimal number in the currency format, then click on the text Behavior After Update

Me.TextBox1.Text = Format (Me.TextBox1, “####, ##. 00”) Apply this code.

Private Sub TextBox1_AfterUpdate()

Me.TextBox1.Text = Format(Me.TextBox1, “####,##”)

End Sub

2. Text Box Date Format –

To convert a value to a date format, double-click on the text box and select Text Behavior After Update. And apply the format code below.


Private Sub TextBox2_AfterUpdate ()

Me.TextBox2.Text = Format (Me.TextBox2, “dd / mm / yyyy”)

End Sub

Excel VBA Tutorial 5 - Text Box Properties, Text Box Currency Format and Date Format

You can use dashes or slashes in the date format or you can use a different format.

dd-mm-yyyy

dd/mmm/yyyy

mmm/dd/yyyy

Similarly, you can apply any format in the text box.

Me. TextBox .Text = Format (TextBox, “Format”)

We get requests now and again asking how to convert numbers to words (or convert currency) e.g. 123.45 becomes One Hundred Twenty Three Dollars and Forty Five Cents. Excel doesn’t provide a function to do this so I’m going to write my own.

Converting Numbers to Words Animation

Microsoft does provide some VBA code which I will use as my starting point, and I have seen other examples, but these examples only allow for one currency.

My code allows you to specify what currency you use, and it takes into account regional variations of the decimal separator. Some countries use . (period or full stop) and some use , (comma).

I’ve created two workbooks that you can use. The first is for use with currency. The second is for general use and will convert the number to a string of words. You can then join that string with whatever you want.

For example, 23,482 could be converted to Twenty Three Thousand Four Hundred Eighty Two people live in this town.

By joining a string like » people live in this town.» to the output from the conversion function, you can use the function however you want.

Download the Code

Sample workbooks with working VBA code can be downloaded from the bottom of this post — just scroll down to the Download Section.

User Defined Function

The code is written as a User Defined Function or UDF. This is just like a regular Excel function that you might use like SUM() and is used in a similar way.

The function is called NumToWords() and you use it in a worksheet by passing a number, or a number stored as text, to it

Passing a number to the function

or you can use it by referencing another cell like this :

Passing a cell reference to the function

The function will take either a string (text) or a number as input and it returns a string (text).

Excel’s Number Limitation

Excel only displays a maximum of 15 digits for a number typed into a cell. Any digits after that are changed to 0.

Excel's 15 Digit Number Limitation

This is a problem if you are working with things like credit card numbers which are typically 16 digits long. You’ll need to use a nifty formula explained in this custom cell format post to solve this particular problem.

This limitation is also an issue if you are working with numbers in the 10’s or 100’s of trillions. Let’s say you want to work out the world’s gross domestic product (GDP) for the last 10 years, which will be into the 100’s of trillions.

What you have to do is store the number as text. Doing this will allow you to work with numbers up to 999,999,999,999,999.99 which is the maximum currently supported by this function.

Other Currencies

If you are not using a dollar as your currency as we do here in Australia, then you can change the currency to whatever you want. Note : this code doesn’t distinguish between countries so you can leave the settings at dollar if you live in the USA, Canada or anywhere else using dollar.

At the top of the code there’s a section where you type in the name of your currency unit and the name of your subunit. For example

Converting different currencies into words

Sub Units of currency

You may have noticed that I’ve allowed for a special case where the name for a single subunit is different to more than one, as is the case with Pounds Sterling.

One subunit is called a penny, more than one would be referred to as pence. So £10.01 would be Ten Pounds and One Penny but £10.02 would be Ten Pounds and Two Pence. This works in a similar way for 2 cents or just 1 cent.

If you use the same term for one and more than one of your subunit then just change the code to reflect this.

Decimal Separator

If you use a , (comma) as your decimal separator then in the same area of code shown above, change the character used for the DecimalSeparator variable.

Update 1 — Spanish Translation

I received a translation of this code into Spanish from Pablo Baez. He said he wanted to share it with his brother in South America, so here it is — thanks Pablo.

The name of the function in Spanish is NumEnPalabras()

Converting Numbers to Words in Spanish

If you have translated the code into other languages, or would like some assistance to do so, please get in touch

Update 2 — Whole Numbers

I also received a request from Khaled Naser Almarwa in Saudi Arabia, asking if the code could be altered to output only whole numbers. Yes it can and you can get this new code here.

This function is called NumToWholeWords()

Converting Numbers to Whole Words

Update 3 — Fractional Sub Units

Chastine asked if the output could be of the form :

One Thousand Two Hundred Dollars and 50/100 only

Yes it can and the new function for this is called NumToWordsFrac()

Download the Sample Workbooks

Enter your email address below to download the workbook with the data and code from this post.

By submitting your email address you agree that we can email you our Excel newsletter.

Понравилась статья? Поделить с друзьями:
  • Vba excel userform activate
  • Vba excel адрес столбца
  • Vba excel winhttp winhttprequest
  • Vba excel user defined type not defined что это
  • Vba excel адрес в строку