For each with GroupItems

ButtFace

Board Regular
Joined
Oct 16, 2015
Messages
63
Office Version
  1. 2016
Platform
  1. Windows
Trying to iterate (for each...next) through specific shape types (form controls, not activex) in a shape group ("grpNav"). Can't get it to work, and the MSDN article is somewhat vague on syntax. What am I doing wrong? :confused:

Code:
Function Nav(Optional Source As Worksheet) As Range
  'decs
  Dim ShapeName As String
  Dim rTarget As Range
  
  Dim rb As OptionButton
  Dim cb As CheckBox
  
  'defs
  On Error GoTo errFatal
  fn "Nav"
  If Source Is Nothing Then Set Source = ActiveSheet
  dp "Source set to: " & Source.Name
  ToggleEvents False
  
  'main
  On Error GoTo errView
  For Each rb In Source.Shapes("grpNav").GroupItems
    ShapeName = Replace(opt.Name, "rb", "") & "."
    With Source
      For Each cb In Source.Shapes("grpNav").GroupItems
        Set rTarget = Nothing
        Select Case UCase(cb.Name)
          Case "CHKTOTALCOST": Set rTarget = Application.Union(IIf(rTarget Is Nothing, .Range(ShapeName & ".Cost.Subtotal"), rTarget), .Range(ShapeName & ".Cost.Subtotal"))
          Case "CHKUNITCOST": Set rTarget = Application.Union(IIf(rTarget Is Nothing, .Range(ShapeName & ".Cost.Unit"), rTarget), .Range(ShapeName & ".Cost.Unit"))
          Case "CHKTOTALHOURS": Set rTarget = Application.Union(IIf(rTarget Is Nothing, .Range(ShapeName & ".Hours.Subtotal"), rTarget), .Range(ShapeName & ".Hours.Subtotal"))
          Case "CHKUNITHOURS": Set rTarget = Application.Union( _
            IIf(rTarget Is Nothing, .Range(ShapeName & ".Hours.ST.Unit"), rTarget), _
            .Range(ShapeName & ".Hours.ST.Unit"), _
            .Range(ShapeName & ".Hours.OT.Unit"), _
            .Range(ShapeName & ".Hours.DT.Unit"), _
            .Range(ShapeName & ".Hours.Difficulty"))
          Case Else: dp "Error: " & cb.Name & " unsupported. Resuming"
        End Select
        rTarget.Hidden = Not (rTarget.Hidden)
skip: Next cb
    End With
  Next rb
  GoTo quit

errView:
  dp "Error: Error intersecting view with category. Resuming"
  Resume skip

errFatal:
  dp "** Error: Unhandled. Terminating"
  GoTo quit
  
quit:
  ToggleEvents True
  fn "Nav", True
End Function
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
How is the code not working?

Also, why do you have 2 loops?
 
Upvote 0
Some more detail about the setup.
On a template page in this workbook, there is a group of shapes called "grpNav".
In grpNav, there are 3 shape types, all form controls: 12 options buttons, 4 checkboxes, and 1 combobox object.
Each option button represents a set of columns (e.g., Material, Freight, Shop, Field, etc...), and each checkbox represents a specific column within that column set (e.g., Material.Cost.Unit, Material.Cost.Subtotal)

The option buttons hide the entire columnset, the checkboxes toggle on/off specific columns within that set.

I get a type mismatch error. Presumably because rb is not a valid object within grpNav. Syntax error?
 
Upvote 0
You can't loop through specific types of controls in GroupItems.

As it is your code is looping through all the controls in the group, twice, and you get a type mismatch because you've declared the loop control variables as specific types of controls.

What you need to do is within the loop check the type of control and act accordingly.
Code:
For Each ctl In Source.Shapes("grpNav").GroupItems
    If TypeName(ctl.OLEFormat.Object) = "CheckBox" Then
        ' do stuff with checkbox
    End If
Next ctl
 
Upvote 0
Blargh. OK. Was hoping to avoid having to loop through the shape group by index. Case TypeName it is then. =\

Thanks for the quick response.
 
Upvote 0

Forum statistics

Threads
1,215,568
Messages
6,125,599
Members
449,238
Latest member
wcbyers

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