What order does a For Each Next loop through my ch

2019-08-14 07:40发布

I have about 20 Check Boxes laid out in a rectangle. How do I know what order my code loops through them with a For Each loop?

I've already confirmed it is not in the order of CheckBox1, CheckBox2, CheckBox3 etc.

1条回答
做自己的国王
2楼-- · 2019-08-14 07:48

For ActiveX checkbox controls laid out on the worksheet, the <right-click>, Order command sets the order they are retrieved in a For ... Each loop. The caveat is that the commands seem to be backwards.

        checkbox_order

  1. Bring to Front will place the shape object at the tail end of the For Each queue.
  2. Send to Back will bring the shape object to the front of the For Each queue.
  3. Bring Forward will shuffle the shape object back one place in the queue.
  4. Send Backward will push the shape object forward one place in the queue.

The VBA for this is in the Shape.ZOrder method.

With Worksheets("Sheet1")
    .Shapes("CheckBox2").ZOrder msoBringForward  '<~~ send back one place in the queue
    .Shapes("CheckBox2").ZOrder msoSendToBack  '<~~ place at the beginning of the queue
End With

Use the Shape.ZOrderPosition property to determine the current position.

查看更多
登录 后发表回答