Changing .Interior.ColorIndex has no effect - excel

In Excel 2003, when I change Series.Interior.ColorIndex to a value I need, it has no effect. It has an effect only when I first manually change color and then run the macro. Apparently this triggers some update mechanism. Does anyone have an explanation for this? Is there a way to somehow trigger this in the chart?.. I.e. make sure that the color changes take effect.
In addition, when I step through the code and watch ColorIndex, it does not change even after value is assigned. Is this one of the many bugs in Excel?
The code looks like this:
Sub DoStuff()
Dim j As Long
For j = 1 To ActiveChart.SeriesCollection.Count
With ActiveChart.SeriesCollection(j)
Select Case ActiveChart.SeriesCollection(j).Name
Case "Milk"
.Interior.ColorIndex = 4
Case "Cookies"
.Interior.ColorIndex = 28
Case "Honey"
.Interior.ColorIndex = 26
End Select
End With
Next j
End Sub
Edit: I am working with bar chart.

Try setting the border as well.
.Interior.ColorIndex = 4
.Border.ColorIndex = 4
.Border.Weight = xlMedium

Edit: In response to comments & edits to the original question:
I laid out an example Excel file, and I was able to get your code to work. Here is how my data is laid out:
| A | B | C | D |
--------------------------------
1 | Milk | 3 | 1 | 4 |
2 | Cookies | 1 | 5 | 9 |
3 | Honey | 2 | 6 | 5 |
And the bar graph looks something like this:
._.
|C|
._.._. |C|._.
._. ._. |C||H| ._.|C||H|
|M|._.|H| ._.|C||H| |M||C||H|
|M||C||H| |M||C||H| |M||C||H|
---------------------------------
1 2 3
Where all of the bars labeled "M" in the above diagram belong to the "Milk" series, all of the bars labeled "C" belong to the "Cookies" series, and all of the bars labeled "H" belong to the "Honey" series.
When I run your code on this chart, the bar colors are changed as expected. Can you tell me what is different between my setup and yours? I'll try to figure it out, but I need more information :)

probably a bug in Excel, which version of Excel are you using?
make sure you service pack/patch it and try again to reproduce the problem.

I had the same problem, using excel 2007 and 2003.
But i fixed it by opening the workbook in 2003, then formatting the data series by right-clicking (etc) and setting it's fill color to Automatic. then when i ran my macro, the colorindex setting part would take hold.

Related

Excel function to round up a number based on 0.15 instead of 0.5

Here's the table of data:
+---+------+---+
| | A | B |
+---+------+---+
| 1 | 5.16 | 6 |
| 2 | 5.15 | 6 |
| 3 | 5.14 | 5 |
+---+------+---+
Column B shows the desired results of rounding a number up or down to nearest whole number based on the threshold of .15, instead of the standard 0.5.
The following function is best I've come up with:
=IF(B1-INT(B1)>=0.15, CEILING(B1, 1), FLOOR(B2, 1))
Is there any other solution involving less nested functions or less length overall?
This is shorter:
=ROUND(B1+0.35,0)
If you are okay with using VBA, you can create a UDF (User Defined Function). Keep in mind that this will require you to save as a Macro-Enabled Workbook.
Function customRound(ByVal dNum As Double) As Double
With Application.WorksheetFunction
If (dNum - Int(dNum)) >= 0.15 Then
customRound = .Ceiling(dNum, 1)
Else
customRound = .Floor(dNum, 1)
End If
End With
End Function
Protip: You can rename the UDF to whatever you would like as long as the name is not already reserved within Excel. In this example, we are simply naming it customRound()
You would then be able to replace your above formula with:
=customRound(B1)
If you are unfamiliar with VBA, read on:
How do I access the VBE?
You can gain access to VBE by pressing Alt + F11 while you are inside your workbook.
Okay, So I have the VBE open. Now how do I apply this UDF?
In the left pane you will see your workbook object modules. This is called the Project Explorer. You will need to create a new module, which you can do by right-clicking inside the Project Explorer > Insert > Module:
Now you can paste the UDF to this new module and you are all set!
Slightly longer:
=IF(RC[-1]-INT(RC[-1])>=0.15,CEILING.MATH(RC[-1]),FLOOR.MATH(RC[-1]))

How to convert Vlookup to Index/match?

Puzzle 1 | Attempt 1 | 0
| |7.1
Suppose I have 3 columns like the above, the blank spaces have nothing in the cell. I want to get the value of 7.1
I've tried
VLOOKUP("Puzzle 1",A:D,1)
To get the value "puzzle 1" and then trying to enclose that in an OFFSET(VLOOKUPVLAUE, 1,2,11) which does not work.
=INDEX(C:C,MATCH("Puzzle 1",A:A,0)+1)
would work for that.

How to label scatterplot points by name?

I am trying to figure out how to get labels to show on either Google sheets, Excel, or Numbers.
I have information that looks like this
name|x_val|y_val
----------------
a | 1| 1
b | 2| 4
c | 1| 2
Then I would want my final graph to look like this.
4| .(c)
3|
2| .(b)
1| .(a)
|__ __ __ __
0 1 2 3 4
Like why can't I label each of these points with its name? I can only seem to label the value, e.g, (c) would show 4
Is the only solution D3?
Well I did not think this was possible until I went and checked. In some previous version of Excel I could not do this. I am currently using Excel 2013.
This is what you want to do in a scatter plot:
right click on your data point
select "Format Data Labels" (note you may have to add data labels
first)
put a check mark in "Values from Cells"
click on "select range" and select your range of labels you want on the points
UPDATE: Colouring Individual Labels
In order to colour the labels individually use the following steps:
select a label. When you first select, all labels for the series should get a box around them like the graph above.
Select the individual label you are interested in editing. Only the label you have selected should have a box around it like the graph below.
On the right hand side, as shown below, Select "TEXT OPTIONS".
Expand the "TEXT FILL" category if required.
Second from the bottom of the category list is "COLOR", select the colour you want from the pallet.
If you have the entire series selected instead of the individual label, text formatting changes should apply to all labels instead of just one.
None of these worked for me. I'm on a mac using Microsoft 360. I found this which DID work:
This workaround is for Excel 2010 and 2007, it is best for a small number of chart data points.
Click twice on a label to select it.
Click in formula bar.
Type =
Use your mouse to click on a cell that contains the value you want to use.
The formula bar changes to perhaps =Sheet1!$D$3
Repeat step 1 to 5 with remaining data labels.
Simple
For all those who don't have the option in Excel (like me), there is a macro which works and is explained here: https://www.get-digital-help.com/2015/08/03/custom-data-labels-in-x-y-scatter-chart/ Very useful
Another convoluted answer which should technically work and is ok for a small number of data points is to plot all your data points as 1 series in order to get your connecting line. Then plot each point as its own series. Then format data labels to display series name for each of the individual data points.
In short it works ok for a small data set or just key points from a data set.
If using VBA is an option and assuming that you have a table named 'Table1' of the form:
Label|x_val|y_val
----------------
a | 1| 1
b | 2| 4
c | 1| 2
this routine should work:
Sub labelDatapoints()
Dim r As Integer
With ActiveSheet.ChartObjects(1).Chart 'The scatter plot
.SeriesCollection(1).ApplyDataLabels
For r = 1 To Range("Table1[Label]").Rows.Count 'iterate through all data points
.SeriesCollection(1).Points(r).DataLabel.Text = Range("Table1[Label]").Cells(r).Value 'add the custom label to the current datapoint
Next r
End With
End Sub
Modified from https://www.get-digital-help.com/dynamic-data-labels-in-a-chart/

VBA HasNextCell function

I have tried searching for a solution but cant find it.
I have a list of products, and each product has many parts. Is there a HasNext function in VBA to see if there are more parts for a product? For instance, for chicken burger, I want to pick out all the parts, put them in an array and display it in another sheet.
I cant hard-code the array, because the client would add in more products in the future. There might be 15, 20, 23 parts etc. Is there a HasNext function to get the value in the next column and add it into the array?
Product | Part 1 | Part 2 | Part 3
Chicken Burger | Veggie | Bun | Patty
You can use Range.End property to detect how "long" the title row is:
Dim col
col=Range("A1").End(xlToRight).Column
For i = 1 to col
If Cells(1, col).Value <> "" Then
'...
End
P.S. I wonder why MSDN refers to it as "property" instead of "method"...

Excel's cell center shifted

I have been working on a math drill program for a bit now. I had asked a similar question to this previously but I wasn't getting any good help with it. So I decided to research the problem more thoroughly and found something interesting.
Now before I begin, I just want to go over the preliminaries. Yes, I am using Microsoft Excel 2007. No, while this may work for you, it is not for me.
Okay now that, that is taken care of:
The problem I am having is when I have
ActiveCell.NumberFormat = "# ?/?"
in my my code it causes the excel's center line to be shifted to the left (that is the only way i know how to explain it).
Meaning, if you have something right justified, it will look centered, if it is centered it will be almost left justified and there is very little difference between left and center justified.
if i have
ActiveCell.NumberFormat = "?/?"
then there is none of the above problems.
The entire code of this section is as follows:
Sub test()
Sheets("sheet1").Select
Range("a1").Select
For i = 1 To 10
ActiveCell.NumberFormat = "# ?/?"
With ActiveCell
.Locked = False
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
End With
ActiveCell.Value = 33
ActiveCell.Offset(0, 1).Select
Next i
End Sub
Any suggestion or reason for why this is happening would be greatly appreciated. Ideally the user should be able to see a mixed number.
Further information, if I change each cell by hand to fraction then it works fine. it is only when excel does it.
It is aligning the number so that the fractional parts are aligned when viewed in columns with a fixed width font.
3 1/3
2
1 3/4
3 2/7
2 2/3
3 1/2
6
EDIT (2)
I tried it with Excel 2007 too, and it makes no difference if I change the NumberFormat by hand or by script.
Anyway, the following script should do what you want. Add it to your Worksheet's source.
It is triggered whenever a value is changed. Then it checks if the correct column is affected (1 in my case), and then it tries to format the number with fractions, but if no fractions result from that, it uses a no-fraction-format.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
For Each cell In Target.Cells
If cell.Column = 1 Then
If IsNumeric(cell.value) Then
cell.NumberFormat = "# ?/?"
cell.Calculate
If InStr(1, cell.Text, "/") = 0 Then
cell.NumberFormat = "#"
End If
End If
End If
Next cell
End Sub
Original post
I've only tried it with Excel 2003, but here is no difference between setting it by hand or via VBA. Please make sure that your code does exactly the same that you do manually, just record a macro of what you do manually and run that on another cell.
The reason for the shift is that Excel reserves that space for the fractions.
1,2 -> | 1 1/5|
1 -> | 1 |
33 -> | 33 |
33,5 -> | 33 1/2|
Is the following what you would like to have?
1,2 -> | 1 1/5|
1 -> | 1|
33 -> | 33|
33,5 -> | 33 1/2|
Why would you like it in that format?
Actually a conditional format would work much better to center everything. See the image below for my setup.

Resources