VBA macro no longer works after Excel 2003->2007 upgrade

SterlingP

New Member
Joined
Oct 6, 2006
Messages
15
Our company just upgraded from Excel 2003 to Excel 2007, and now one of my macros no longer works. It is a simple macro to change text in some text boxes to red if the value in a specified cell is a negative number. There are no error messages or anything, it just doesn't work. Any ideas?

Code:
Sub RedIfNegative()

Dim fnt As Font

Set fnt = Worksheets("Results").Shapes("Text Box 1").TextFrame.Characters.Font
If (Worksheets("Results").Range("F5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 2").TextFrame.Characters.Font
If (Worksheets("Results").Range("L5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 3").TextFrame.Characters.Font
If (Worksheets("Results").Range("O5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 4").TextFrame.Characters.Font
If (Worksheets("Results").Range("I5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 5").TextFrame.Characters.Font
If (Worksheets("Results").Range("R5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 6").TextFrame.Characters.Font
If (Worksheets("Results").Range("U5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Nothing

End Sub
 
Last edited:
A more fundamental question is why use code at all?

You can link the contents of a text box to a cell. Select the text border (not the contents) then click in the formula bar and type = Then click in a cell that you want linked to the text box.

In that cell enter the formula =IF(F5<0,"{My message}","")

Now, if F5 < 0 the linked cell will contain your message and so will the text box.

Of course, permanently set the font of the text box to red.

Voila! Same effect. No code.

Of course, if you can do with just the cell and don't need any property specific to a text box, you can even do away with the text boxes. {grin}

Our company just upgraded from Excel 2003 to Excel 2007, and now one of my macros no longer works. It is a simple macro to change text in some text boxes to red if the value in a specified cell is a negative number. There are no error messages or anything, it just doesn't work. Any ideas?

Code:
Sub RedIfNegative()

Dim fnt As Font

Set fnt = Worksheets("Results").Shapes("Text Box 1").TextFrame.Characters.Font
If (Worksheets("Results").Range("F5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 2").TextFrame.Characters.Font
If (Worksheets("Results").Range("L5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 3").TextFrame.Characters.Font
If (Worksheets("Results").Range("O5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 4").TextFrame.Characters.Font
If (Worksheets("Results").Range("I5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 5").TextFrame.Characters.Font
If (Worksheets("Results").Range("R5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Worksheets("Results").Shapes("Text Box 6").TextFrame.Characters.Font
If (Worksheets("Results").Range("U5").Value < 0) Then
  fnt.Color = RGB(255, 0, 0)
Else
  fnt.Color = RGB(255, 255, 255)
End If

Set fnt = Nothing

End Sub
 
Upvote 0

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
A more fundamental question is why use code at all?

You can link the contents of a text box to a cell. Select the text border (not the contents) then click in the formula bar and type = Then click in a cell that you want linked to the text box.

In that cell enter the formula =IF(F5<0,"{My message}","")

Now, if F5 < 0 the linked cell will contain your message and so will the text box.

Of course, permanently set the font of the text box to red.

Voila! Same effect. No code.

Of course, if you can do with just the cell and don't need any property specific to a text box, you can even do away with the text boxes. {grin}

I think I get what you are saying, but I don't just want the value to display in the text box if it's negative - I want it to display in the text box regardless of whether positive or negative, but for the text to be in red if the value is negative. Kind of like a conditional format for a text box. I've tried giving the cells with the values that display in the text boxes conditional formats and then linking the text boxes to those cells, and they pick up the values and number formats just fine, but not the conditional format that changes the color.

Also, I tried as you suggested earlier and both CreateBasketTrees and RedIfNegative are definitely executing. It is the strangest thing - I can run the RedIfNegative manually and it works perfectly, but it never works if run from inside CreateBasketTrees, even if I take out the call to RedIfNegative and replace it with the exact code from the RedIfNegative macro.
 
Upvote 0

Forum statistics

Threads
1,215,472
Messages
6,125,004
Members
449,203
Latest member
Daymo66

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top