How do I use different currencies VBA [duplicate] - excel

I want to format all cells of a column. The data is a number and I want to add a decimal from left keeping only two digits after that. Then format it as per currency standards. For eg:
Data: Output:
10000 100.00
112233 1,122.33
123456789 1,234,567.89
The requirement is normal and output is in italics.

The following will format a given column as Currency:
Sheets("Sheet1").Columns("A").NumberFormat = "£#,##0.00"

Macro recorder is a bit dangerous in this case, because if you use it, your code would look like this (from the other answer):
Sheets("Sheet1").Columns("A").NumberFormat = "£#,##0.00"
However, this is only ok in UK. If you want to write code, which formats currency based on the local settings of the PC, then it is a good idea to use the currency there:
Sheets("Sheet1").Columns("A").Style = "Currency"
Then a UK person would get pounds format and a US one dollars.

Related

How can I convert currency on Excel Sheet?

I wanna convert currency to a specific currency on excel sheet, how can i do this. I wanna convert dollar to euro.
Simply multiply by a rate, eg day rate (depends on the context which is not specified)
https://www.xe.com/currencyconverter/convert/?Amount=1&From=USD&To=EUR
This is rather a financial than technical question.
Rgds,
W
PS Lukaku's market value is higher than Pogba's

How to format fantasy currency in Microsoft Excel?

In the Format Cells dialog of Excel, where you can create custom numbers, how do I format modern American currency to look like fantasy currency? Instead of dollars, dimes, and pennies, I need it to show gold pieces, silver pieces, and copper pieces (gp, sp, cp, respectively), while also excluding any that results in 0.
Some examples of the desired output:
$25.85 = 25gp 8sp 5cp
$75.50 - 75gp 5sp
$1,000.00 = 1,000gp
Does that make any sense?
This seems to do what you want according to your sample data.
=TRIM(TEXT(INT(A2),"0 \g\p ")&IF(--RIGHT(TEXT(A2, "0.0")),TEXT(--RIGHT(TEXT(A2, "0.0")),"0 \s\p "),TEXT(,))&IF(--RIGHT(TEXT(A2, "0.00")),TEXT(--RIGHT(TEXT(A2, "0.00")),"0 \c\p "),TEXT(,)))
Actually, that formula can be improved to,
=TRIM(TEXT(INT(A2),"0 \g\p ;;;")&TEXT(--RIGHT(TEXT(A2,"0.0")),"0 \s\p ;;;")&TEXT(--RIGHT(TEXT(A2,"0.00")),"0 \c\p ;;;"))

Hundred separator in Libre office calc number formating / Indian number formatting

I am moving to Open office spreadsheets and I need to handle large financial values in spread sheet cell.
By default libre office provide NUMBER & CURRENCY format, where "," is used as per Indian Numbering system(refer https://en.wikipedia.org/wiki/Indian_numbering_system )
However, I don't need the number till one's unit place. I need to see number in crore.
26,75,73,350 should be shown as 25.76
Libre office only provide "," as thousand separator. This separator follow US counting system, i.e. millons etc. If used as user-defined format "0.00,,"
I see cell value as 267.57 and not 26.76
What is a good user-defined format following Indian counting system?
I am not sure but try this, N4 contains the value 12345678
=ROUNDUP(N4/("1" &REPT(0,LEN(N4)-2)),2)
Result 12.35
You can changethe bold section with a fixed value 10000 or so
With a formula it would just be a matter of dividing by 10000000 (ten million) and showing decimal places to suit. Unfortunately Open Office Calc (like most other popular spreadsheet programs) does not provide such a format by default. However in a custom format , effectively divides by 1000 (one thousand) and % multiplies by 100 (one hundred) so 267573350 with a User-defined format of:
0.00,,,%
looks like 26.76%. The underlying value is however retained, so for example adding 1 to such a cell (in a different cell of general format) would show 267573351.
That is about as close as I can get just with formatting, but since % (ab)used this way is so open to confusion you might want to give some visual warning by say appending "C" to the format (0.00,,,%"C") to show:
26.76%C

Wrong format of a cell in Excel

I am copying large tables from a website to Excel and it is copied in a wrong format. The number which I need seems to be in a cell but when I click into the cell it shows me that it is a date (and the number shown in the cell is its month and year).
Is it possible to change that so that the number in the cell will be 4.25 instead of 1.4.2025?
Thanks a lot.
You can change the format by highlighting the cells, right clicking selecting the format cells option. It will not change the string of numbers from 1.4.2025, but you will be able to replace the date format with something more appropriate for your needs. *
In the 1900 date system, 1.12 and 12.15 (for example) if entered as 1/12 and 12/15 respectively (where the locale has . as the decimal separator) into cells formatted General will show 01-Dec and Dec-15 respectively. The underlying number however (ie change format from Custom to Number) is 42339 for both. 42339 in Short Date format shows as 01/12/2015.
Although generally possible to deduce what malformed number gave rise to what looks like a date it is not always possible to do so. Ambiguity is greatest where the digits for the decimal or integer parts are less than 13. In addition, the locale in which the numbers were entered may play a part (since, in the 1900 date system, 3/4 for example would be interpreted as 42097 where the date convention is DMY 'UK' and 42067 where the date convention is MDY 'US').
So the answer to your question:
Is it possible to change that so that the number in the cell will be 4.25 instead of 1.4.2025?
is "Yes, but not fully reliably" (and even so would require knowledge or guessing of locale, date system and time of the conversion). Since you are "copying large tables from a website to Excel" it seem you retain access to the source data and I would recommend returning to that. On a small scale manual adjustments may be viable, on a large scale consider importing first to software without such automatic interpretation (eg Word, NotePad++) or, if possible, as character separated values imported as text. Address the delimiters with string handling there and only after that import to Excel or save in Excel format.

.NumberFormat sometimes returns the wrong value with dates and times

It seems that every week or so someone posts a question about dates being converted (corrupted?) to American format. Like many others, I have attempted to help but the problem is elusive. I now wonder if I have discovered the cause.
I am working on an application in which I need to extract data from an Excel worksheet and output it as strings formatted to match the value the Excel user can see. So if the value is “1” formatted to display as “1.00” then I want the string to be “1.00”.
I achieve this effect by testing the cell value to be a number, date or time. If it is, I retrieve the number format and use it to format the cell value so:
With .Cells(Row, Column)
Output string = Format(.Value, .NumberFormat)
End With
In most cases this gives me exactly the output I require. However, sometimes I get American dates and times when the source is formatted as a UK date or time.
After much experimentation with Excel 2003 and Excel 2007, I have discovered the cause. (I do not have access to Excel 2010 but from questions I deduce it has the same problem.) This question is in part intended to reveal this problem to the world because I can discover nothing on the internet to indicate that anyone else has noticed it. (No doubt someone will reply that they googled “xyz” and got the answer immediately.) However, the main purpose of this question is to seek suggestions for obtaining the result I need in all situations.
Typically I enter dates as, for example, “23mar12”. Excel recognises this as a date and formats it as “23-Mar-12”. I can select Format Cells and enter or select a custom format or select one of the date formats so I can have any format I can imagine wanting including non-English names for days and months.
However, in one case the format I select is not the format that is recorded: Custom format “dd/mm/yyyy” is recorded as Date format “* 14/03/2001”. This is not obviously a problem until further down the line.
I created a column of dates and times and formatted each with a different custom or standard format. I wrote a macro to extract the NumberFormat for each of these dates and times and write it as a string to an adjacent column. I also formatted the value using the number format and wrote that string to a third column.
In a number of cases the format selected and recorded by Excel was not the format returned by NumberFormat:
Excel format NumberFormat
Date: * 14/03/2001 m/d/yyyy
Date: * 14 March 2001 [$-F800]dddd, mmmm dd, yyyy
Date: 14/03/2001 dd/mm/yyyy;#
Date: 14/03/01 dd/mm/yy;#
Date: 14/3/01 d/m/yy;#
Date: 14.3.01 d.m.yy;#
Date: 2001-03-14 yyyy-mm-dd;#
Date: 14 March 2001 (1) [$-809]dd mmmm yyyy;#
Date: 14 March 2001 (2) [$-809]d mmmm yyyy;#
Custom: hh:mm:ss h:mm:ss
Time: * 13:30:55 [$-F400]h:mm:ss AM/PM
Time: 13:30:55 (1) hh:mm:ss;#
Time: 13:30:55 (2) h:mm:ss;#
Time: 01:30:55 PM [$-409]hh:mm:ss AM/PM;#
Time: 1:30:55 PM [$-409]h:mm:ss AM/PM;#
The values (1) and (2) in the Excel format column were added by me to indicate that there are two apparently identical formats. As can be seen from the NumberFormat column, in each case the second version suppresses a leading zero.
Most changes have no important effect. “[$-F800]” and so on are apparently dummy values with no effect. Apparently you can replace “F800” with an Microsoft country code to have the names of days and months translated to the language of that country.
However, the three standard formats that Microsoft marks with an asterisk are changed unacceptably. The dates are changed from little endian to middle endian; the time is changed from 24 hour to 12 hour and the day of the week has been added to “* 14 March 2001”.
The asterisk against the dates, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), date formats that you apply do not switch date orders with the operating system.” The asterisk against the time, references the comment: “Except for items that have an asterisk () in the Type list (Number tab, Format Cells dialog box), time formats that you apply do not switch time orders with the operating system.”
If I have to, I can warn my users that standard date and time formats may not give the result desired. However, if they want the popular format “dd/mm/yyyy”, they cannot have it. “dd-mm-yyyy”, for example, is OK but custom format “dd/mm/yyyy” becomes date format “* 14/03/2001” becomes “m/d/yyyy”.
Returning to my opening point: is this strange handling of one particular date format the reason so many people claim their dates are sometimes being converted to American format and is this why the problem is so elusive? I have come across this type of problem elsewhere of one group of Microsoft programmers not knowing what another group are doing. Is this why some functions always work and other sometimes don’t? Some Microsoft programmers know where to look for the correct format and others don’t?
More importantly, for me, can anyone suggest:
How I obtain the true date or time format?
Some other way of determining the user’s chosen display format for a date or time?
BTW 1: I recall that thirty or so years ago I was told that the American military do not use month/day/year format; only American civilians use this format. Can anyone tell me if this is true?
BTW 2: The similar problem is with Excel colours. Excel holds its colours as "ggbbrr" while everybody else holds them as "rrggbb". The programmers for the .Net Excel inter-op were not told and and did not reverse the Excel colour number before using it to control the screen.
I have mainly come up against formatting and date issues when opening text files which have been saved with different regional settings. Two useful cell properties for dealing with this are:
.Text returns the cell value as it is displayed
.Value2 returns the unformatted cell value or date serial number.
As you say, standard date and number formats depend on windows regional settings and this may not be desired behavior as the same workbook can display differently in different regions. MS introduced the regional code prefixes in number formats (circa Excel 2000?) which enforce consistent display if needed but they need to be explicitly selected.
If you really want to see a date or number as the user entered it, you could extract the contents of the .xlsx file looking at the worksheet cell format and the shared strings xml definitions which list the number formats in the saved workbook. I don't really see a need to do this though as the underlying value is stored internally as a serial number and this will not change.
BTW 1: It's been almost 30 years since I was in the military...
I worked on helicopters and I was taught to use a format such as this in the aircraft logbooks: 3 Apr 12. So, that's how I still write dates. This way, there's no wondering about 4/3/2012 - is it April 3 or March 4?
I hacked this: I rewrite the original data in a known format. it relies on DateSerial and TimeSerial:
'Google spreadsheet stores dates in USA format (MM/DD/YYYY). We're in Australia, using DD/MM/YYYY, so we need to swap them.
'
With dc 'the cell who contains a date in USA format.
d = .Value 'capture value in USA format
t = TimeValue(d)
.NumberFormat = "dd/mm/yyyy" 'set to OZ format, so Excel knows the values were swapped in its internal math.
.Value = DateSerial(Year(d), Month(d), Day(d)) 'DateSerial takes y,d,m. We swap Month and Day components, to get OZ format dates
.Value = .Value + TimeSerial(Hour(t), Minute(t), Second(t))
dc.Font.Bold = True ' We bold the cells that are swapped, for debugging
End With
End If

Resources