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

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

Related

Multi-select Lookup Field data to VBA

Ultimately I'm going to want to make a whole bunch of checkboxes enabled&unlocked/disabled&locked based on which values are checked in a multi-select lookup, and how many are allowed. To do this, first I need to know how to get at the data in these multi-select lookups through vba. I imagine.
I've tried to set a variable in vba to the value of one of these fields, but the only data type it seems to accept is variant - and even then I can't get the immediate window to tell me what I've collected into that variable.
I need vba to be able to work out if a certain label is one of the ones listed in the multi-select field.
Now for further explanation/context, real examples: Lets say you can pick colours for a book based on what author and genre the book "owns". Each author and genre has a variable number of colours you can pick, and a limited range.
Lets say the book is by Bob, who is allowed an 2 of red, blue, orange and yellow, and it's a fantasy book, which can be 3 of red, white, grey, black and purple.
All of the checkboxes on the form for red, blue, yellow, orange, white, grey, black and purple become enabled and unlocked, while all the rest remain locked and disabled as they aren't available at all.
The user selects red - no change, except for that box being selected.
The user also selects blue - no change, except for that box being
selected, because while 2 (max allowed) of bob's colours are
selected, red is also on the fantasy list, which still has 2
remaining. The overlap saves more.
The user de-selects red, and selects yellow - red is still available
(fantasy) but orange is now disabled and locked.
The user selects red, white and purple - grey and black are disabled
and locked, as all options have now been exhausted.
Now, if you want to start me on all of how that'll work, fantastic, but all I'm really asking at this point is how to get hold of the data in those multi-select fields. The rest is for context etc.
The .Value property of a multi-select field, either a recordset or a control, is defined as a variant array in Access. This means you need to use array functions to get the value.
You can read the value like this:
rs.Fields("MyMultiValuedField").Value(0) 'First value
Join(rs.Fields("MyMultiValuedField").Value, ", ") 'Concatenated values
And set the value like this:
rs.Fields("MyMultiValuedField").Value = Array("Value1", "Value2")
Note that in a lookup field, the keys are stored, not the values being looked up. And in my experience, MVFs have minimal validation when assigning using VBA, you can even insert values of the wrong type leading to garbage.

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

Get the id of the selected value in a dropdown validation list on Excel in VBA

Using Excel, lets say I have a validation list made of 5 values like this one :
Patate
Tomate
Courgette
Concombre
Patate
In a cell containing a drop down list made of these 5 value, I select the fifth value : "Patate".
I want to get in VBA that this cell contains the 5th value of my validation list. Something like :
x = Cell.Validation.GetIDValueSelected
x = 5
I can't use Vertical search because I might have 2 or even more time the same value in my list (too long to explain why).
This list is also dynamics (depending of another sheets) so it doesn't always contains 5 values.
I hope I made it clear for everyone to understand my needs but I will be glad to add more information if needed.
Thank you for your time.
Sadly, once you have used DV to fill a cell with junk, there is no way to tell which piece of junk you picked:
You would have to pad each piece of junk with a different number of blanks.

Number to be copied from cell with the same number format and show it as a label for a shape without any change in the number format

I have a cell in excel which contains a value, lets say 100000.
Now i want this value to have commas in between them to represent the thousands and millions i.e. 100,000. I can do this by changing the number format in the home menu.
Now i want this value to be copied from that cell and paste it as a label for a shape. When i am doing this the commas go away showing me just the numbers.
I want it to happen through VBA but this is not happening in excel itself.
Does anyone have a plausible solution for this?
In range object use Text property, like this:
Sheet1.Shapes(1).TextFrame.Characters.Text = Range("A1").Text

VBA - split cell value by font colour

I've been given a spreadsheet with a range of cells, each of which contains a list of numbers.
Each number has been given a font colour, so in one cell you might have two orange numbers, a red one and a green one. I need to treat each colour differently; for example, I can count green and red numbers as they are, but I need to see if each orange number occurs in previous cells in the range before counting it.
The number lists are comma-separated, so getting individual numbers shouldn't be a problem, but how do you retain and work with the colour information?
I'd post code, but frankly I'm not sure where to start.
Thanks in advance!
Solved using this:
http://www.mrexcel.com/forum/excel-questions/656265-excel-visual-basic-applications-count-items-cell-color-font.html
Essentially a loop that uses InStr to find commas in a string (in this case, the contents of a cell), then looks at the colour of the next character after the comma. I've just edited it to say if the ColorIndex = 46, copy the subsequent characters into a space in an array. Then I can look at each item in the array, and compare it with other cells in the range to see if it appears elsewhere.