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:

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Are your 2007 macros enabled? I know that Excel files in 2007 with macros have to be saved as xlsm files.
 
Upvote 0
I forgot to mention - the macro is being called as a function from inside another sub. It runs fine if I set it up as a sub itself and run it as a separate macro, but when changed to a function and called from another sub using "Call RedIfNegative", it does not work.
 
Upvote 0
What do you mean, when changed to a function? You can call it from another sub, but your code will never be a function. Make sure that the heading says sub, not function - and try putting that and the calling sub into the same module.
 
Upvote 0
What do you mean, when changed to a function? You can call it from another sub, but your code will never be a function. Make sure that the heading says sub, not function - and try putting that and the calling sub into the same module.

Sorry, I think I was unclear.

The original code I had was a function, i.e. Function RedIFNegative() <code> End Function. It was called from a Sub called CreateBasketTrees. Withough putting all the other code in, it looked like this:

Code:
Sub CreateBasketTrees()
   Call RedIfNegative
End Sub

Function RedIfNegative()
  <code>
End Function()

Both CreateBasketTrees and RedIfNegative are in the same module.

Upon learning that RedIfNegative was no longer working after the upgrade, I changed the Function RedIfNegative to a Sub to see if it worked correctly when I ran in independently, which it did. It does not work if called as a function as shown above, or even when putting the RedIfNegative code inline in the CreateBasketTrees sub.
 
Upvote 0
What I was initially suggesting is that you make the call the same way you indicated in post # 5, but that you change "Function RedIfNegative" to "Sub RedIfNegative."

I must be missing the point here, though... what is RedIfNegative supposed to do?

It looks like it declares a variable, sets it and resets it a few times, then sets it to nothing - but never passes anything back out.
 
Upvote 0
How is CreateBasketTrees executed?
Sorry, I think I was unclear.

The original code I had was a function, i.e. Function RedIFNegative() <code> End Function. It was called from a Sub called CreateBasketTrees. Withough putting all the other code in, it looked like this:

Code:
Sub CreateBasketTrees()
   Call RedIfNegative
End Sub

Function RedIfNegative()
  <code>
End Function()

Both CreateBasketTrees and RedIfNegative are in the same module.

Upon learning that RedIfNegative was no longer working after the upgrade, I changed the Function RedIfNegative to a Sub to see if it worked correctly when I ran in independently, which it did. It does not work if called as a function as shown above, or even when putting the RedIfNegative code inline in the CreateBasketTrees sub.
 
Upvote 0
What I was initially suggesting is that you make the call the same way you indicated in post # 5, but that you change "Function RedIfNegative" to "Sub RedIfNegative."

I must be missing the point here, though... what is RedIfNegative supposed to do?

It looks like it declares a variable, sets it and resets it a few times, then sets it to nothing - but never passes anything back out.

You are right, there is no particular reason for it to be a function since it does not return a a value. I can't recall why I originally coded it that way, but the behavior is the same when RedIfNegative is a sub - when called from the CreateBasketTrees sub, it doesn't work, when executed manually from the Macro menu, it does.
 
Upvote 0
This should not matter but what happens if you replace
Code:
   Call RedIfNegative
with
Code:
   RedIfNegative

Also, put a
Code:
Debug.Print "In CreateBasketTrees"
in CreateBasketTrees and make sure it is actually executed. Put a similar statement in RedIfNegative and check if it is even called.

I just execute it manually from the Macros menu.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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