0

スプレッドシートには、既存のチェックボックスが多数あり、それぞれのリンクされたセルを手動で設定するのは面倒な作業です。

多数のそれらをグループ化し、次のことを実現する VBA コードを記述したいと考えています。

i = 1
n = number of checkboxes in group
While i < n
Loop
For checkbox i in 'group X', assign linked cell to cell (range A1-->Z1)
End loop

明らかにこれはVBAではありませんが、私は構文に精通していません.a)そのような機能を実行できるかどうか(つまり、グループ化された要素+リンクされたセルの割り当てによる)b)ルックアップする必要があるコマンド/構文それを書くこと。

どうもありがとう

4

2 に答える 2

6

このコードはあなたが望むことをします:

Sub linkFromGroup()
Dim g              ' we put groups in this variable
Dim gc As Integer  ' group count - number of elements in group
Dim r As Range     ' points to cell we will link to            

Set r = Range("A1") ' initially point to cell A1 - this could be anything

' we will know something is a group when we can count the objects in the group
' if we try to count objects that don't exist we will get an error.
' we will trap that with the following line:
On Error Resume Next 

' turn off screen updating while macro runs - or it will flicker
Application.ScreenUpdating = False

' loop over all the "shapes" in sheet1. A group is a special kind of shape
For Each g In Sheets("Sheet1").Shapes
  ' set count to zero
  gc = 0
  ' see if we get a value other than zero
  gc = g.GroupItems.Count ' on error we go to the next line and gc will still be zero
  If gc > 0 Then
    For ii = 1 To gc
      g.GroupItems.Item(ii).Select
      Selection.LinkedCell = r.Address  ' right now I am assuming only check boxes in groups...
      Selection.Caption = "linked to " & r.Address ' not necessary - but shows which box was linked to what. 
      Set r = r.Offset(1, 0) ' next check box will be linked with the next cell down from r
    Next ii
  End If
Next g

Application.ScreenUpdating = True ' turn on normal operation again  

End Sub

これを実行した後のテスト シートの例 (2 つのグループと 1 つのチェック ボックスがありました):

ここに画像の説明を入力

単一のチェック ボックスは変更されませんでした。グループは変更されました。ボックス $A$8 をクリックしたことがないため、その値は TRUE または FALSE として表示されません。

VBA エディター (Alt-F11) を開き、モジュールを挿入して、上記のコードを貼り付ける必要があります。次に、(Alt-F8) を使用して実行し、表示されたリストからマクロを選択します。これを行う方法は他にもたくさんあります。あなたの質問から、ここからコードを適応させることができるようです。最初にスプレッドシートのコピーでこれを行うようにしてください - これが意図したとおりに機能することを確認するまでは!

于 2013-05-14T00:53:58.183 に答える
4

それはあなたが探しているものですか?

以下のコードは、シート 1 の各チェックボックスをチェックし、セル A1 などから始まるリンクされたセル プロパティを設定します。

シンプルにしましょう。

Sub sample()
    Dim i As Integer
    Dim chk As Variant

    i = 1

    With Sheets("Sheet1")

        For Each chk In .OLEObjects
            If TypeName(chk.Object) = "CheckBox" Then
                chk.LinkedCell = .Range("A" & i).Address
                 i = i + 1
            End If
        Next

    End With
End Sub
于 2013-05-14T00:53:51.570 に答える