Excel VBA | Referencing grouped objects, based on Long variable

Young Grasshopper

Board Regular
Joined
Dec 9, 2022
Messages
58
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi world,

I have a bunch of text boxes grouped together with other objects, and want to write a code that referance the grouped objects trough a long variable.

The grouped objects is named "PreviewGroup1", and have a text box called "HeadlinesPreview" grouped toghether with multiple other stuff.
Let's say I want to make a with statment. I'm trying to do something like this:

VBA Code:
Sub Preview()
With ThisWorkbook.Worksheets("CustomerPreview").Shapes("PreviewGroup1")

.Shapes("HeadlinesPreview").TextFrame.Characters.Text = "x"

End with
End

But I only get error messages...
How should this with statment be?

Also;
Let's say i have multiple groups called PreviewGroup1, PreviewGroup2, PreviewGroup3, PreviewGroup4 etc.
would it be possible to referance them trough a Long variable like:

VBA Code:
Dim i as Long

For i = 1 to 10
With ThisWorkbook.Worksheets("CustomerPreview").Shapes("PreviewGroup(i)")

This above probobly don't make sense, but hopefully explains somewhat what I'm trying to say.

Would appreciate any help:)
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
For the first one, you can use the GroupItems property of the Shape object...

VBA Code:
    With ThisWorkbook.Worksheets("CustomerPreview").Shapes("PreviewGroup1")
        .GroupItems("HeadlinesPreview").TextFrame.Characters.Text = "x"
    End With

For your second one . . .

VBA Code:
    Dim i As Long
    Dim shp As Shape
    
    For i = 1 To 3
        Set shp = ThisWorkbook.Worksheets("CustomerPreview").Shapes("PreviewGroup" & i)
        'etc
        '
        '
    Next i

. . . where again you can use the GrouptItems property of the Shape object to access your grouped items.

Hope this helps!
 
Upvote 1
Solution
You're very welcome, I'm glad I could help.

Cheers!
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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