PDA

View Full Version : Change background color of Cells on same row



steambop
05-28-2013, 05:05 AM
I am trying to work with a table in MS word. My goal is to change the background cell color of the adjacent cell to the right depending on the value entered in the corresponding textbox.

The code I have so far is:
TableRef = ActiveDocument.Range(0, Selection.Tables(1).Range.End).Tables.Count
Mytable = ThisDocument.Tables(TableRef)
rowref = Selection.Cells(1).RowIndex
colref = Selection.Cells(1).ColumnIndex

If TextBox1.Value > 10 Then
ThisDocument.Tables(TableRef).Cells(rowref, (colref + 1)).Shading.BackgroundPatternColor = wdColorGreen
end if
This code should color the background of the adjacent cell in the row green if the textbox value is above 10.
The problem is that I can't seem to select any cell that is on the currently active row (It always automatically selects the same cell with the textbox). I have tried entering in the cells reference manually (without using rowref or colref) but that doesn't work either. It can however change the background color of any other cell in table, as long as it is not on the active row.

Any ideas on what could be causing this? Help is much appreciated.

Thanks

fumei
05-28-2013, 03:43 PM
So you have a textbox in a cell. What kind of textbox? Why are you using a textbox in a cell?

What are you using to fire the Sub?

gmaxey
05-28-2013, 04:42 PM
He is using an ActiveX textbox and while I can confirm the issue, I can't explain it. There is a workaround:

Private Sub TextBox1_Change()
Dim oRng As Word.Range
On Error GoTo Err_ReEntry
If TextBox1.Text > 10 Then
Set oRng = Selection.Range
Selection.Cells(1).Range.Select
Selection.Cells(1).Next.Range.Shading.BackgroundPatternColorIndex = wdBrightGreen
oRng.Select
Else
Err_ReEntry:
Set oRng = Selection.Range
Selection.Cells(1).Range.Select
Selection.Cells(1).Next.Range.Shading.BackgroundPatternColorIndex = wdAuto
oRng.Select

End If
End Sub

steambop
05-29-2013, 12:38 AM
The textbox is an ActiveX one. It is activated everytime the user presses return in the textbox.

@gmaxey
I also found that it does work using selection, however I am trying to avoid using the selection funtion.

Any other ideas on how to get this to work?

gmaxey
05-29-2013, 04:57 AM
No. I think the problem is that the ActiveX control has the focus. That does'nt explain why it happens, but I think it will limit your options.

fumei
05-29-2013, 12:58 PM
It is definitely because the ActiveX object has the focus...IF you are doing the action while in the textbox.

Here an alternative to Greg's code, although it is not significantly different. It does not use Selection however, at least nothing is actually SELECTED.
Private Sub TextBox1_Change()
Dim oCell As Cell
On Error GoTo Err_ReEntry
Set oCell = Selection.Range.Cells(1)
If TextBox1.Text > 10 Then
oCell.Next.Range.Shading.BackgroundPatternColorIndex = wdBrightGreen
Else
Err_ReEntry:
oCell.Next.Range.Shading.BackgroundPatternColorIndex = wdAuto
End If
End Sub

gmaxey
05-29-2013, 01:16 PM
Gerry,

No joy here with Word 2010. That produces the same result. The field with the TextBox gets shaded (not the next adjacent field).

Odd thing though is that with any of the other methods and yours, I can set the text in the adjacent field. This focus issue seems to affect only certain processes.

Private Sub TextBox1_Change()
Dim oCell As Cell
On Error GoTo Err_ReEntry
Set oCell = Selection.Range.Cells(1)
If TextBox1.Text > 10 Then
oCell.Next.Range.Shading.BackgroundPatternColorIndex = wdBrightGreen
oCell.Next.Range.Text = "I'm over 10"
Else
Err_ReEntry:
oCell.Next.Range.Shading.BackgroundPatternColorIndex = wdAuto
oCell.Next.Range.Text = ""
End If
End Sub

fumei
05-29-2013, 04:40 PM
It does not work with 2010? Hmmmm. It works fine with 2002. Hmmmm. Interesting. I wonder why not. Hmmmm. Just one more reason that 2010 sucks.

gmaxey
05-29-2013, 04:47 PM
Word 2003 is in your camp. By your logic Word 2007, 2010, and 2013 all suck.

steambop
05-30-2013, 01:58 AM
Well I am glad to here other people are having the same problem. It is a really annoying little bug that gets in the way of what I am trying to do.

I also need to have this working for all version of MS word so I think using Selection is going to be the safest bet.

Thanks for all the input.

fumei
05-30-2013, 12:47 PM
I was - kind of - kidding.

But yeah, 2007, 2010 and 2013 suck. Not totally, but I really AM wondering what on earth would be so different in the object model that would make things this off.

After all they both are still based on:

Selection.Range.Cells(1)

and Next. It seems odd.