Error with simple code - changing text box formats

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
683
Office Version
  1. 2016
Platform
  1. Windows
The following code doesn't work, can anyone tell me why and let me know what code would work. The code errors at the line '.TextFrame.Characters.Font.Color = RGB(Colors(0), Colors(1), Colors(2))'
Many thanks.

VBA Code:
Dim R As Long, G As Long, B As Long, Colors As Variant
    
    Sheet97.Select
    
    Colors = Split(Range("Z93"), ",")
    With ActiveSheet.Shapes.Range(Array("TextBoxNeg5.5", "TextBoxNeg3.5"))
        .TextEffect.FontSize = Range("W93").Value
        .TextFrame.Characters.Font.Color = RGB(Colors(0), Colors(1), Colors(2))
        .TextFrame.AutoSize = True
    End With
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
The arguments in the RGB function have to be integers. Try:
VBA Code:
        .TextFrame.Characters.Font.Color = RGB(Int(Colors(0)), Int(Colors(1)), Int(Colors(2)))
 
Upvote 0
The arguments in the RGB function have to be integers. Try:
VBA Code:
        .TextFrame.Characters.Font.Color = RGB(Int(Colors(0)), Int(Colors(1)), Int(Colors(2)))
Many thanks for your response.
Still getting an error on this line using your code unfortunately. The original code works fine when it's only referring to one Text Box, ie. With ActiveSheet.Shapes("TextBoxNeg5.5"), it's only a problem when I change the code to several Text Boxes, if this helps.
 
Upvote 0
So, when you put the 2 shapes in the array, the TextFrame property doesn't seem to work. To prove this, I commented out the Font.Color line and the AutoSize line gave the same error. If you "select" the 2 frames, your code for those 2 lines will work. However, you can't use the Text Effect on the "Selection". Someone smarter than me (shouldn't be hard to find) probably understands this and how to work around it, but the below appears to work for me.
VBA Code:
    With ActiveSheet.Shapes.Range(Array("TextBoxNeg5.5", "TextBoxNeg3.5")))
        .TextEffect.FontSize = 15
        .Select
    End With
    Selection.Font.Color = RGB(Colors(0), Colors(1), Colors(2))
    Selection.AutoSize = True

    Range("A1").Select
 
Upvote 0
Solution
So, when you put the 2 shapes in the array, the TextFrame property doesn't seem to work. To prove this, I commented out the Font.Color line and the AutoSize line gave the same error. If you "select" the 2 frames, your code for those 2 lines will work. However, you can't use the Text Effect on the "Selection". Someone smarter than me (shouldn't be hard to find) probably understands this and how to work around it, but the below appears to work for me.
VBA Code:
    With ActiveSheet.Shapes.Range(Array("TextBoxNeg5.5", "TextBoxNeg3.5")))
        .TextEffect.FontSize = 15
        .Select
    End With
    Selection.Font.Color = RGB(Colors(0), Colors(1), Colors(2))
    Selection.AutoSize = True

    Range("A1").Select
Brilliant! That does the job, spot on.
Thank you very much for your time on this, much appreciated. :)(y)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,249
Members
452,623
Latest member
Techenthusiast

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