| Home | What's New |
生産性向上 基本テクニック |
トラブル回避 テクニック |
生産性向上 リンク |
personal.xls 強化講座 |
生産性向上 ツール |
Site Map |
Coffee Break |
Guest Book |
地球風 画像館 |
Q&A
Salon EXCELの質問はこちらへ |
| 4.右クリックメニューの追加カスタマイズ |
11/27/2000 -
|
|
personal.xls にせっかくオリジナルの便利なマクロを登録しても、それを起動するのに毎回「ツール」メニューから目的のマクロ名を探し出して「実行」ボタンを押すのはスマートではありません。 |
|
2004.10.3 マクロのコードを一部改定しました。 |
|
なお、マクロ中の FaceID に指定している番号ですが、私は Susumu KAWAMURA's homepage というホームページに掲載されている画像一覧表を参考にさせていただいています。ここから、上図のメニュー名称の左に表示されるアイコンとしてふさわしいものを探して、指定しています。 |
|
|
|
|
| Sub auto_open() Add_RightClickMenu 1 'auto_open に既に他のマクロが記述されている場合には、 'この1行だけを追加する End Sub Sub Add_RightClickMenu(num%) '標準メニューの上に追加
' auto_openに追加するのを忘れずに!
Dim i As Long
Dim cstBar As CommandBar
Dim wcb As CommandBar
'
' Application.CommandBars("cell").Reset
' Set cstBar = CommandBars("cell")
' cstBar_sub cstBar
'
' Application.CommandBars("row").Reset
' Set cstBar = CommandBars("row")
' cstBar_sub cstBar
'
' Application.CommandBars("column").Reset
' Set cstBar = CommandBars("column")
' cstBar_sub cstBar
'
' For i = 23 To 28 'Excel97の場合は
21 to 26 にして下さい
'Excel2003の場合は
29 to 34 にして下さい
' Application.CommandBars(i).Reset
' Set cstBar = CommandBars(i)
' cstBar_sub cstBar
' Next
i = 0
For Each wcb In CommandBars
i = i + 1
Select Case wcb.Name
Case "cell", "Cell", "column", "Column", "row", "Row"
Application.CommandBars(i).Reset
Set cstBar = CommandBars(i)
cstBar_sub cstBar
' Case Else
' DoEvents
End Select
Next
End Sub
Sub cstBar_sub(cstBar As CommandBar)
With cstBar
.Controls.Add Type:=msoControlButton, Before:=1
.Controls(1).Caption = "ウィンドウの上下整列(&1)"
.Controls(1).OnAction = "ウィンドウの上下整列"
.Controls(1).FaceId = 298
.Controls(1).BeginGroup = True
.Controls.Add Type:=msoControlButton, Before:=2
.Controls(2).Caption = "アクティブシートの複数画面表示(&2)"
.Controls(2).OnAction = "同一Sheetの複数画面表示"
.Controls(2).FaceId = 585
.Controls.Add Type:=msoControlButton, Before:=3
.Controls(3).Caption = "セル縦位置中央揃え(&3)"
.Controls(3).OnAction = "セル縦位置中央揃え"
.Controls(3).FaceId = 2062
.Controls(3).BeginGroup = True
.Controls.Add Type:=msoControlButton, Before:=4
.Controls(4).Caption = "セル縦位置上詰め(&4)"
.Controls(4).OnAction = "セル縦位置上詰め"
.Controls(4).FaceId = 2061
.Controls.Add Type:=msoControlButton, Before:=5
.Controls(5).Caption = "列幅で折り返し(&5)"
.Controls(5).OnAction = "列幅折り返し表示"
.Controls(5).FaceId = 119
.Controls.Add Type:=msoControlButton, Before:=6
.Controls(6).Caption = "全シートをHOMEポジションに(&6)"
.Controls(6).OnAction = "To_Home"
.Controls(6).FaceId = 1826
.Controls(6).BeginGroup = True
.Controls.Add Type:=msoControlButton, Before:=7
.Controls(7).Caption = "入力後のセル移動方向の変更(&7)"
.Controls(7).OnAction = "セル移動方向切替"
.Controls(7).FaceId = 133
.Controls.Add Type:=msoControlButton, Before:=8
.Controls(8).Caption = "枠線の表示切替(&W)"
.Controls(8).OnAction = "枠線表示切替え"
.Controls(8).FaceId = 217
.Controls.Add Type:=msoControlButton, Before:=9
.Controls(9).Caption = "行列番号の表示切替(&G)"
.Controls(9).OnAction = "行列番号表示切替"
.Controls(9).FaceId = 800
.Controls.Add Type:=msoControlButton, Before:=10
.Controls(10).Caption = "A1形式R1C1形式の切替(&A)"
.Controls(10).OnAction = "A1_R1C1"
.Controls(10).FaceId = 503
.Controls.Add Type:=msoControlButton, Before:=11
.Controls(11).Caption = "最近使用したファイルの一覧に追加(&E)"
.Controls(11).OnAction = "Add_RecentFiles"
.Controls(11).FaceId = 462
.Controls.Add Type:=msoControlButton, Before:=12
.Controls(12).Caption = "全ての隠しシートを表示する(&Q)"
.Controls(12).OnAction = "全シート表示"
.Controls(12).FaceId = 2587
.Controls(12).BeginGroup = True
.Controls.Add Type:=msoControlButton, Before:=13
.Controls(13).Caption = "シートを確認しながら非表示にする(&R)"
.Controls(13).OnAction = "シート隠蔽"
.Controls(13).FaceId = 1641
.Controls.Add Type:=msoControlButton, Before:=14
.Controls(14).BeginGroup = True
End With
'
End Sub
|
Sub auto_open() Add_RightClickMenu_2 1 'auto_open に既に他のマクロが記述されている場合には、 'この1行だけを追加する End Sub Sub Add_RightClickMenu_2(num%) '標準メニューの下に追加
' auto_openに追加するのを忘れずに!
Dim i As Long
Dim cstBar As CommandBar
Dim wcb As CommandBar
'
' Application.CommandBars("cell").Reset
' Set cstBar = CommandBars("cell")
' cstBar_sub_2 cstBar
'
' Application.CommandBars("row").Reset
' Set cstBar = CommandBars("row")
' cstBar_sub_2 cstBar
'
' Application.CommandBars("column").Reset
' Set cstBar = CommandBars("column")
' cstBar_sub_2 cstBar
'
' For i = 23 To 28 'Excel97の場合は
21 to 26 にして下さい
'Excel2003の場合は
29 to 34 にして下さい
' Application.CommandBars(i).Reset
' Set cstBar = CommandBars(i)
' cstBar_sub_2 cstBar
' Next
i = 0
For Each wcb In CommandBars
i = i + 1
Select Case wcb.Name
Case "cell", "Cell", "column", "Column", "row", "Row"
Application.CommandBars(i).Reset
Set cstBar = CommandBars(i)
cstBar_sub_2 cstBar
' Case Else
' DoEvents
End Select
Next
End Sub
Sub cstBar_sub_2(cstBar As CommandBar)
Dim i%
i = cstBar.Controls.Count + 1' With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "選択したセルの情報(&L)"
.Controls(i).OnAction = "CellsInformation"
.Controls(i).FaceId = 343
.Controls(i).BeginGroup = True
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "ウィンドウの上下整列(&1)"
.Controls(i).OnAction = "ウィンドウの上下整列"
.Controls(i).FaceId = 298
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "アクティブシートの複数画面表示(&2)"
.Controls(i).OnAction = "同一Sheetの複数画面表示"
.Controls(i).FaceId = 585
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "セル縦位置中央揃え(&3)"
.Controls(i).OnAction = "セル縦位置中央揃え"
.Controls(i).FaceId = 2062
.Controls(i).BeginGroup = True
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "セル縦位置上詰め(&4)"
.Controls(i).OnAction = "セル縦位置上詰め"
.Controls(i).FaceId = 2061
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "列幅で折り返し(&5)"
.Controls(i).OnAction = "列幅折り返し表示"
.Controls(i).FaceId = 119
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "全シートをHOMEポジションに(&6)"
.Controls(i).OnAction = "To_Home"
.Controls(i).FaceId = 1826
.Controls(i).BeginGroup = True
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "入力後のセル移動方向の変更(&7)"
.Controls(i).OnAction = "セル移動方向切替"
.Controls(i).FaceId = 133
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "枠線の表示切替(&W)"
.Controls(i).OnAction = "枠線表示切替え"
.Controls(i).FaceId = 217
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "行列番号の表示切替(&G)"
.Controls(i).OnAction = "行列番号表示切替"
.Controls(i).FaceId = 800
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "A1形式R1C1形式の切替(&A)"
.Controls(i).OnAction = "A1_R1C1"
.Controls(i).FaceId = 503
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "最近使用したファイルの一覧に追加(&E)"
.Controls(i).OnAction = "Add_RecentFiles"
.Controls(i).FaceId = 462
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "全ての隠しシートを表示する(&Q)"
.Controls(i).OnAction = "全シート表示"
.Controls(i).FaceId = 2587
.Controls(i).BeginGroup = True
End With
'
i = i + 1
With cstBar
.Controls.Add Type:=msoControlButton
.Controls(i).Caption = "シートを確認しながら非表示にする(&R)"
.Controls(i).OnAction = "シート隠蔽"
.Controls(i).FaceId = 1641
End With
'
End Sub
|
| 2004.7.10 参考資料 <コマンドバーコレクションのバージョン別一覧> | |||||||||||
上記のマクロで、 For i = 23 To 28 'Excel97の場合は
21 to 26 にして下さい
'Excel2003の場合は
29 to 34 にして下さい
という部分がありますが、これは右クリックメニューを追加するセル・行・列のコマンドバーコレクションでの位置がExcelのバージョンによって異なるために、Excek97の場合とExcel2000/2002の場合とExcel2003の場合でコーディングを変えないといけないためです。 上記のマクロはバージョンの違いを吸収するための判定文を入れていませんが、いろいろなバージョンのExcelで実行される可能性があるマクロを作成する場合は、バージョンによる相違を意識しておく必要があるでしょう。 そこでバージョンの違いを意識するマクロを作成するための参考資料として、コマンドバーコレクションの番号(インデックス値で参照する場合の値)と英語名称・日本語名称のバージョンごとの一覧をExcelシートに作成してみました。 |
|||||||||||
commandbarlist.xls をダウンロードする(右クリックして「対象をファイルに保存(A)」してください) 下の<注意>も参照してください。 |
|||||||||||
この一覧は、 以下のマクロを各バージョンのExcelで実行した結果を1つのシートにまとめて、さらにバージョンによる相違が分かりやすいように行方向に同じものが並ぶように調整したものです。
Sub CommandBarList()
Dim cbar As CommandBar
Dim i As Integer
i = 1
For Each cbar In CommandBars
Cells(i, 1).Value = cbar.Name
Cells(i, 2).Value = cbar.NameLocal
i = i + 1
Next
End Sub
commandbarlist.xls を見ていただくと、今回右クリックメニューを追加しているCommandBarオブジェクトの "Cell"
, "Column" , "Row" が、Excel97では21〜26番目に、Excel2000と2002では23〜28番目に、Excel203では29〜34番目にあることが分かると思います。日本語名称は、Excel97では半角カタカナになっていますが、Excel2000以降は全角カタカナになっています。 |
|||||||||||
2004.10.3 <注意> 同じバージョンのEXCELでも、この"Cell"や"Column"などの出現場所が異なるケースがあるようです(実際に同僚のPCで確認しました)。したがって、上に添付した commandbarlist.xls の内容(順番)は、バージョンによって固定ではなく変化する可能性があるようです。原因までは調べていませんが。 そのため、いろいろなバージョン・環境で使用されるマクロの場合は、今回のコード改訂のように、番号ではなく名前で検索して操作するほうが安全かもしれません。 |
|||||||||||
|
Excelのバージョンを調べるには、ApplicationオブジェクトのVersionプロパティ Application.Version を調べます。バージョン番号が文字列型の値で返ってきますので、それをIF文等で判定や分岐させて、操作するコマンドバーコレクションの番号をバージョンごとに変化させればよいでしょう。 |
|||||||||||
|
|||||||||||