3
2
I would like to have the following format in a cell in Excel:
123g 99s 99c
100 copper (c) = 1 silver (s)
100 silver = 1 gold (g)
So that, if I enter 12394
it'll be shown as "1g 23s 94c".
Is that possible somehow?
3
2
I would like to have the following format in a cell in Excel:
123g 99s 99c
100 copper (c) = 1 silver (s)
100 silver = 1 gold (g)
So that, if I enter 12394
it'll be shown as "1g 23s 94c".
Is that possible somehow?
6
Try this custom format:
#0"g" #0"s" 00"c"
Note that this will show all three "units" regardless of the actual amount, so 12
will be shown as "0g 0s 12c". If you only want to show up to the biggest "unit" with non-zero amount (so 12
would be "12c", 598
would be "5s 98c", etc), you can tweak it as follows, to actually apply different formats based on the value:
[>=10000]#0"g" #0"s" 00"c";[>=100]#0"s" 00"c";#0"c"
It's not perfect, for instance 10245
will be shown as "1g 02s 45c", rather than "1g 2s 45c", because it just splits up the cell value rather than dividing it by 100 to get the individual amounts, but other than that it should do what you want.
To handle negative values, you're going to have to combine the above format with conditional formatting, because Excel can only have up to 3 conditions in a single number format. Try the following (I'm assuming Excel 2010, should also work in 2007, not sure about earlier versions):
=A1>=0
, then click on "Format..."[>=10000]#0"g" #0"s" 00"c";[>=100]#0"s" 00"c";#0"c"
=A1<0
, then click on "Format..."[<=-10000]-#0"g" #0"s" 00"c";[<=-100]-#0"s" 00"c";#0"c"
If you need to modify or remove the formatting rules, or expand them to apply to more cells, just go to "Conditional formatting" → "Manage rules".
Another option is to use a VBA function to format the values. Press Alt+F11 to open the VBA editor, right-click in the Project window and select Insert → Module, then paste the following code (just one of many ways to write such a function, of course):
Function MyFormat(intValue)
Dim strReturn As String
If intValue < 0 Then strReturn = "-"
intValue = Abs(intValue)
If intValue > 10000 Then
strReturn = strReturn & Fix(intValue / 10000) & "g"
intValue = intValue - (Fix(intValue / 10000)) * 10000
End If
If intValue > 100 Then
If Len(strReturn) > 0 Then strReturn = strReturn & " "
strReturn = strReturn & Fix(intValue / 100) & "s"
intValue = intValue - (Fix(intValue / 100)) * 100
End If
If Len(strReturn) > 0 Then strReturn = strReturn & " "
strReturn = strReturn & intValue & "c"
MyFormat = strReturn
End Function
This approach affords you greater flexibility than using custom number formats, and it's easier to use than managing conditional formatting rules - just type =MyFormat(12345)
instead of 12345
. The downside, however, is that you lose the ability to perform mathematical operations with the data, because Excel now sees the values as text, not numbers.
0
The following custom format will do what you want:
??\g ??\s ??\c
For anyone wondering, this format also works in LibreOffice Calc and in Google Spreadsheets (
[>=10000]#0"g" #0"s" 00"c";[>=100]#0"s" 00"c";#0"c"
- in my case I don't need to worry about negatives). – Amos M. Carpenter – 2015-12-23T07:37:34.143That's just fine. Thanks a lot, oh you excel wizard... – Feroc – 2012-09-04T17:29:35.190
Ah, very nice! I was trying to figure out how to do that, but all I had found so far used Conditional Formatting. – SaintWacko – 2012-09-04T17:32:34.003
About your tweak: That's great for positive values, but for negative values it shows only copper anymore. – Feroc – 2012-09-04T17:52:11.950
@Feroc Ah, I didn't think about negative values. See the updated instructions in my answer. – Indrek – 2012-09-04T18:21:50.830
Oha... ok, I think then I just go with the first solution. Don't want to make it too complicated. Thanks a lot, great help. – Feroc – 2012-09-04T18:56:56.707