[[20190725045345]] 『チェックボックスにチェックしたら関連するものに』(くろ) ページの最後に飛ぶ
[
初めての方へ |
一覧(最新更新順) |
全文検索 |
過去ログ ]
『チェックボックスにチェックしたら関連するものにもチェックを入れる』(くろ)
閲覧有難うございます。
初めて利用させていただきます。
現在作成中のVBAが初めて作成するものになります。
チェックボックスにチェックを入れたら関係するものにもチェックを入れたいのですが
関数の使用方法が間違っているのか「型が一致しません」と表示されます。
関数も型の宣言もよくわかっていないため何を間違っているのか、どう記述すればいいのかご教授ください。
例としては
グループ1(セルA1)(チェックボックスのリンクセルD1)
・ホットケーキ(セルA2)(チェックボックスのリンクセルD2)
・ソフトクリーム(セルA3)(チェックボックスのリンクセルD3)
・ショートケーキ(セルA4)(チェックボックスのリンクセルD4)
グループ2(セルB1)(チェックボックスのリンクセルE1)
・アイスクリーム(セルB2)(チェックボックスのリンクセルE2)
・プリン(セルB3)(チェックボックスのリンクセルE3)
・クレープ(セルB4)(チェックボックスのリンクセルE4)
でソフトクリームにチェックを入れたらアイスクリームにもチェックが入ったり外したりしたいのです。
逆にアイスクリームにチェックを入れたらソフトクリームにチェックが入ったり外したりもしたいです。
また、可能であれば下記に記載するMacro1とMacro2を簡略化する方法があればご教授ください。
セルがTrueになったらリンクしてるセルのArray変数を判別+Macro1のgroup1を自動的に切り替わるようにしたいです。
コードは
Sub Macro0()
Dim group1 As Variant
Dim group2 As Variant
Dim group3 As Variant
Dim group4 As Variant
Dim group5 As Variant
Dim grofile As Variant
group1 = Array("・アイスクリーム", "・ソフトクリーム")
group2 = Array("・ホットケーキ")
group3 = Array("・ショートケーキ")
group4 = Array("・プリン")
group5 = Array("・クレープ")
grofile = 0
Macro1
Macro2
End Sub
Private Sub Macro1()
If grofile = Filter(group1, Cells(2, "A")) And Cells(2, "D") = True Then
If grofile = Filter(group1, Cells(2, "A")) Then Cells(2, "D") = True
If grofile = Filter(group1, Cells(3, "A")) Then Cells(3, "D") = True
If grofile = Filter(group1, Cells(4, "A")) Then Cells(4, "D") = True
If grofile = Filter(group1, Cells(2, "B")) Then Cells(2, "E") = True
If grofile = Filter(group1, Cells(3, "B")) Then Cells(3, "E") = True
If grofile = Filter(group1, Cells(4, "B")) Then Cells(4, "E") = True
ElseIf grofile = Filter(group1, Cells(2, "A")) And Cells(2, "D") = False Then
If grofile = Filter(group1, Cells(2, "A")) Then Cells(2, "D") = False
If grofile = Filter(group1, Cells(3, "A")) Then Cells(3, "D") = False
If grofile = Filter(group1, Cells(4, "A")) Then Cells(4, "D") = False
If grofile = Filter(group1, Cells(2, "B")) Then Cells(2, "E") = False
If grofile = Filter(group1, Cells(3, "B")) Then Cells(3, "E") = False
If grofile = Filter(group1, Cells(4, "B")) Then Cells(4, "E") = False
End If
End Sub
Private Sub Macro2()
If grofile = Filter(group2, Cells(2, "A")) And Cells(2, "D") = True Then
If grofile = Filter(group2, Cells(2, "A")) Then Cells(2, "D") = True
If grofile = Filter(group2, Cells(3, "A")) Then Cells(3, "D") = True
If grofile = Filter(group2, Cells(4, "A")) Then Cells(4, "D") = True
If grofile = Filter(group2, Cells(2, "B")) Then Cells(2, "E") = True
If grofile = Filter(group2, Cells(3, "B")) Then Cells(3, "E") = True
If grofile = Filter(group2, Cells(4, "B")) Then Cells(4, "E") = True
ElseIf grofile = Filter(group2, Cells(2, "A")) And Cells(2, "D") = False Then
If grofile = Filter(group2, Cells(2, "A")) Then Cells(2, "D") = False
If grofile = Filter(group2, Cells(3, "A")) Then Cells(3, "D") = False
If grofile = Filter(group2, Cells(4, "A")) Then Cells(4, "D") = False
If grofile = Filter(group2, Cells(2, "B")) Then Cells(2, "E") = False
If grofile = Filter(group2, Cells(3, "B")) Then Cells(3, "E") = False
If grofile = Filter(group2, Cells(4, "B")) Then Cells(4, "E") = False
End If
End Sub
Macro1の2行目
If grofile = Filter(group1, Cells(2, "A")) And Cells(2, "D") = True Then
で先ほども書きましたが「型が一致しません」と表示されます。
If文の記述方法かFilterの使い方を間違えていると思うのですがどう直したらいいかわからない状況です。
元々のコードは
・Arrayに複数の値が入っている
・グループとグループ内のアイテムはさらに多い
・「プロシージャが大きすぎます」と表示されたためマクロを呼び出すように変更
・そのため呼び出すマクロが多い
・Macro1とMacro2のようなコードが更に35個近くある
・このようなコードになっている理由は今後も変数の値とグループを更新する必要があるため
となっています。
説明がわかりづらいですが宜しくお願い致します。
< 使用 Excel:Excel2010、使用 OS:Windows7 >
Filterで配列に含まれる文字列の個数を取得しようとしているようですが、
そこがまず違います
型が一致しません というのは、
grofileが数値、Filter以下が配列なので比較できないという意味です
Filter は 配列を返しますので
添え字をつけてあげないと値は取得できません
下記のコードを実行してみてください
Sub test1()
Dim a As Variant
a = Filter(Array("A", "B", "C"), "B")
MsgBox a 'エラーになります
MsgBox a(0) 'エラーになりません
End Sub
Filter の結果で、判断したいなら
以下のコードで動作確認してみてください
Sub test2()
If UBound(Filter(Array("A", "B", "C"), "D")) = -1 Then
MsgBox "配列中に指定した文字列はありません"
Else
MsgBox "配列中に指定した文字列はあります"
End If
If UBound(Filter(Array("A", "B", "C"), "B")) = -1 Then
MsgBox "配列中に指定した文字列はありません"
Else
MsgBox "配列中に指定した文字列はあります"
End If
End Sub
(渡辺ひかる) 2019/07/25(木) 09:07
コメント返信:
[ 一覧(最新更新順)
]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.