Share via

Macro Recorder Macro throws error when run

Anonymous
2022-12-26T17:39:42+00:00

I recorded a macro while changing the fill color in a Shape in my spreadsheet. The code recorded is as follows:

Sub Macro3()

'

' Macro3 Macro

' '

ActiveSheet.Shapes.Range(Array("Rounded Rectangle 2")).Select 

With Selection.ShapeRange.Fill 

    .Visible = msoTrue 

    **.ForeColor.ObjectThemeColor = msoThemeColorAccent5** 

    .ForeColor.TintAndShade = 0 

    .ForeColor.Brightness = 0.400000006 

    .Transparency = 0 

    .Solid 

End With 

End Sub

When this macro is run it throws an error on the statement in red. The message is:

I don't think this should be happening in an unmodified macro. Maybe a bug?? Any idea how I can get around this?

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2023-01-09T16:30:15+00:00

    For everyone's info:

    The problem turned out to be that the Microsoft Office 16.0 Object Library needed to be added to the References.

    That made the problem go away.

    Kudos to OssieMac for his suggestion!

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2022-12-27T00:11:42+00:00

    Thanks for raking the time to make such a comprehensive response!

    In further testing Watch shows that in a new workbook msoThemeColorAccent5 Value resolves to a 9, which is why both your code and mine work fine.

    In the problem workbook Watch shows that msoThemeColorAccent5 Value is shown as Empty, which is why neither your code nor mine work. They will work if I hard code a 9 instead of using msoThemeColorAccent5.

    So now the question is: Why does msoThemeColorAccent5 have no Value in the problem workbook?

    Both the test and problem workbooks use an unmodified Office Theme and Colors.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2022-12-26T22:04:10+00:00

    Hi,

    I'm Sneha and I'd be happy to help you out with your question. Sorry for the inconvenience caused.

    It looks like there is an issue with the ObjectThemeColor property of the ForeColor object. The error message indicates that the specified value is out of range, which means that the value you are trying to set for the ObjectThemeColor property is not valid.

    There are a few possible reasons why this error might be occurring:

    1. The value you are trying to set for the ObjectThemeColor property is not one of the predefined theme colors. The ObjectThemeColor property can only be set to one of the predefined theme colors, which are represented by constants in the msoThemeColor enumeration. Make sure you are using a valid constant from this enumeration.
    2. The theme you are using does not have an accent color with the index you are specifying. The msoThemeColorAccent5 constant corresponds to the fifth accent color in the current theme. If the current theme does not have an accent color with this index, you will get an error.
    3. There is an issue with the Shape object itself. It's possible that the Shape object you are trying to modify does not exist, or there is some other issue with it that is causing the error.

    To troubleshoot this issue, you can try the following:

    1. Make sure you are using a valid constant from the msoThemeColor enumeration for the ObjectThemeColor property.
    2. Check the current theme to make sure it has an accent color with the index you are specifying. You can do this by going to the Page Layout tab in the ribbon and clicking on the "Colors" button in the "Themes" group. This will open the "Colors" dialog, where you can see all the accent colors in the current theme.
    3. Make sure the Shape object you are trying to modify exists and is in a valid state. You can check this by adding some debugging code to your macro to print out the name and type of the Shape object. You can also try selecting the Shape object manually to see if there are any issues with it.

    Here is the corrected code that should work without any errors:

    Sub Macro3() Dim shape As Shape

    Set shape = ActiveSheet.Shapes("Rounded Rectangle 2") With shape. Fill . Visible = msoTrue . ForeColor.ObjectThemeColor = msoThemeColorAccent5 . ForeColor.TintAndShade = 0 . ForeColor.Brightness = 0.4 . Transparency = 0 . Solid End With End Sub

    In this code, I have declared a variable shape of type Shape, and used the Set statement to assign the Shape object with the name "Rounded Rectangle 2" to this variable. I have then modified the properties of the Fill object of the shape object, rather than the Selection object.

    This should fix the error you were seeing, and allow you to change the fill color of the Shape object as intended.

    If you have any other questions or need assistance with anything, please don't hesitate to let me know. I'm here to help to the best of my ability.

    Give back to the Community. Help the next person who has this issue by indicating if this reply solved your problem. Click Yes or No below.

    Best Regards, Sneha

    Was this answer helpful?

    0 comments No comments