Shapes.Name 値の名前を、必要に応じて使用できる名前に変更します。
Excel で図形のグループを作成する場合、たとえば四角形を例にとると、最初のオブジェクトを右クリックし、[数式] タブに移動して [名前の定義] をクリックします。pup-up ウィンドウの下部に次の行があります。 [ ="Rectangle 73" ] を参照 これは VBA Shapes.Name 値です。(ここで何かを行っても、VBA Shapes.Name の値は変更されません)
個々のオブジェクト名を使用することもIF Application.Caller.Name = "Rectangle 73"
、VBA で整形した名前をより便利なものに変更することもできます。
それぞれの形を手で作ると「Rectangle 73」「Rectangle 74」「Rectangle 75」・・・コピペすると「Rectangle 73」「Rectangle 102」「Rectangle 103」 ... これで Shapes.Name 値が得られたので、必要に応じて VBA で再定義できます。
Sub nameShapes()
Dim shp As String
Dim shpType As String
Dim myName As String
'original root name of group shapes *do not include trailing space*
shpType = "Rectangle"
'new name of group shapes
myName = "newName"
'first number value of new shape names
n = 1
'number values of first, second, and last shapes
n1 = 73
n2 = 103
nx = 124
'active code --------------------------------------
'--------------------------------------------------
shp = shpType & " " & n1
ActiveSheet.Shapes(shp).Name = myName & " " & n
For x = n2 To nx
n = n + 1
shp = shpType & " " & x
ActiveSheet.Shapes(shp).Name = myName & " " & n
Next n
'--------------------------------------------------
'active code --------------------------------------
End Sub
このコードを独自の個別のモジュールに配置し、値を入力して、RunSub の VBA ウィンドウ ツールバーの再生ボタンをクリックするだけです。
n = Right(Application.Caller, 2)
これで、クリックされたボタンの数値を取得するために使用できます。必ず定義してくださいDim n As Integer
。
異なるボタン グループが同じ関数を呼び出す場合Select Case Left(Application.Caller, 7)
、 shape.name の最初の 7 文字を取得するために使用し、Case "newName"
そのボタン グループに固有のアクションを実行するために使用できます。