Formatting in excel showing -0.02 as -0 - formatting

I have applied following format on excel cell. ##0\%;[Red]\(##0\%\)
This format is rounding off the value and showing negative number in red color within brackets.
issue: small values such as -0.03 is shown as (0%) instead of coming as 0%. showing 0 as negative does not make any sense.
What should i do to force -0 to be shown as 0 instead of (0).
Thank you

It will color and parenthesise your number based on its actual value, not how it displays.
Hence, it you don't want that showing up, you need to change the actual value, such as with:
=trunc(<current formula>,0)
or (most likely better):
=round(<current formula>,0)
Alternatively, you can apply conditional formatting to a cell so that the given formatting is only applied if, for example, round(<this cell>,0) = 0.
In that case, you could apply "##0\%" if the rounding results in 0 or your current format "##0\%;[Red]\(##0\%\)" otherwise.

Related

vba customize cells format

I am needing to customize cells with simple thousands format, like 1000, without any separator or decimal.
However, I wish to remove text fonts other than a number when they are input.
For example, I want to input 120118, however in my paper from which I am copying that figures, it is formatted as a date, thereby 12/01/18. I am needing Excel to simply keep it as 120118 after typing, removing the slash (/). I have seen similar settings in access queries.
Have you tried simply pasting only the cell value with:
Selection.PasteSpecial Paste:=xlPasteValues
Or just clear the cell format and format it again with your desired format.
Try:
Selecting the range
Home > Number > Number Format (or Ctrl+1 I think) > Custom
Enter ddmmyy
Okay
Can be done programmatically e.g.
Thisworkbook.worksheets("Sheet1").range("A1:A50").numberformat = "ddmmyy"
The above would only be a visual/cosmetic change and the internal value of each cell would still be a date (technically a number) for calculation purposes.
However, if I've misunderstood and you instead want to go from the date 21 Jan 2018 to the number 210118, I think you would need to get the range's value(s), format as DDMMYY string, then clng() - or maybe (DD*10000) + (MM*100) + (YY) might work, then format as "000000" to preserve leading zeros.

Excel interpreting 0% as a large number

I am encountering a strange error when trying to use VBA to implement some formulas. Here is the formula I'm using to place "0.00%" instead of an error message in my cells:
ActiveWorkbook.Sheets("SM Summaries").Range("F6").FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1], ""0.00%"")"
Now it places the 0.00% just fine, but even when its cell type is defined as percentage, if I sort largest to smallest by % it places the 0s at the top...
Here you can see where the 0s are placed on top even when doing largest to smallest. And the cell types are set properly to percentage as well:
Any idea what's going on here?
I have also tried just placed in 0.00 instead of 0.00% as well as 0% and "0", all of those options still showing 0 as being considered a "large number". The only reason that they're not green in the picture above is because I set a conditional formatting rule to stop turning 0s green (when the number is greater than 1).
The ""0.00%"" is putting the value in as text. And as such when Excel Sorts Descending it puts Text first.
You need to just put in 0 and format the cells as percent. So replace the ""0.00%"" with 0. No quotes. The quotes are forcing the number to text.
ActiveWorkbook.Sheets("SM Summaries").Range("F6").FormulaR1C1 = "=IFERROR(RC[-2]/RC[-1], 0)"

Flexible Data Label Formatting

I'm hoping to add custom data labels to a stacked bar chart in Excel 2010. Each bar will have a label value pulled from a value in the sheet which is not necessarily equal to the height of the bar itself.
The sheet I'm making is a template and has drop-down selections to change the units of the data. For example: $MM with 1 decimal point, K with 2 decimal points, $ with 0 decimal points, etc... I'm running into an issue formatting the labels to match the data formatting selections. I've tried two approaches:
Formatting the data labels themselves using VBA. Partial code below for $K with one decimal:
`With ActiveChart.SeriesCollection(x).Points(y)`
.DataLabel.Text.NumberFormat = "$#,##0.0,"
Unfortunately, the labels don't seem to read the commas and divide by one thousand, so I can't accurate display thousands or millions. A value of 1000 in the above code will display as $1,000.0 instead of the desired $1.0.
Format the values in the worksheet, and then just read them with VBA. Basically, I use conditional formatting to properly display the values on the sheet, and then read them with .DataLabel.Text = Cells(r,c)
Unfortunately, though the data is formatted correctly, the values are unchanged and the labels display with no custom formatting at all.
Is there any reasonable way to achieve my goal? I'm hoping there's a way to either (1) make commas count when changing data label number formats or (2) change the actual values of the data in my sheet based on the formatting drop-down selections (instead of just giving them custom formatting which leaves the value unchanged).
Thank you,
Lance
I think the words "conditional formatting" are your issue here: I've had problems with VBA reading the underlying format of a cell when it's conditionally formatted (admittedly, my issue was with colour, rather than number format, but I expect the reasons for it not working would be the same).
If you adopt your second approach, i.e. relying on the worksheet's format using the chart's "Linked to source" option, then on a trigger of when the drop-down option is changed, you could use VBA to set the format on the worksheet instead of using conditional formatting - then the chart would pick up whatever format you're using.
Please try this:
?Format(1000,"$#,##0.0,")
$1.000,0
?Format(1000,"$#,##0,.0")
$1,0
?Format(1234,"$#,##0,.0")
$1,2

Conditional formatting in rounded values

Facing a trouble in Cognos 10, in my report I have to apply conditional formatting for values less than zero (except zero) as red, for which I am also applying round-off to zero decimals. Now the problem is values like -0.1 are getting rounded-off to 0, but also shown in red due to conditional formatting.
Is there any work around to show the rounded zero values in black?
Try rounding off the value on your condition on conditional style too. Like these:
round(([ColumnValue]*100),0) < 0
then set the foreground color or background color to red. Hope it helps.

ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor returns -1

I have a Word 2007 doc with a lot of tables. Each table has certain cells filled with 2 custom colors. I created a macro which takes for input 3 values to feed the RGB function, creating one of the colors, matches it against each of the colors, and changes the match with a new color.
I used an If statement that compares the color of the filled cell with the color returned by the RGB function, feeding the function with the input given by the user.
If ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor = RGB(inputRed, inputGreen, inputBlue) Then
'code
end if
As I was looking through the document to see the results, I noticed that a few cells from 3 tables were left with the old color, so apparently the macro could not recognize it.
In Word 2007 I selected the cell that was supposed to have its color changed. I went to Home -> Shading -> More colors -> Custom and saw the 3 values that matched perfectly with the 3 values given as input by the user.
After 30 minutes of staring at the monitor, I thought about going to the doctor to have my eyes examined. :)
Before doing that though, I started to debug the app. on each of the cells that were not changed. Upon examining this line:
ActiveDocument.Tables(k).Range.Cells(j).Shading.BackgroundPatternColor
I saw that it returned -1.
I am thinking -1 is the equivalent of null or nil, meaning that either the cell is not filled, but if so, why can I see the RGB values? Or perhaps the system can't read the RGB values, but who is this system exactly?
Do you have a link where the Shading.BackgroundPatternColor method is well explained?
I don't know about a value of -1, but colours in Word 2007 changed slightly so that the values were no longer necessarily RGB values. I have written extensively on this - please see http://www.wordarticles.com/Articles/Colours/2007.php