0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

エクセルマクロをリボンから起動してみる(執筆途中)

Last updated at Posted at 2025-07-07

完全な備忘録


マクロにいちいちショートカット設定するのめんどくさいし

重要なファイル触ってる時に間違って起動しちゃうのこわいし

ってことでアドイン使ってリボンから起動できるようにする


探せば先駆者がいると思うけどあとから柔軟に追加とかできるようにしてあるから そこはまあいい点な気がする

まずはマクロ

テストマクロ.xlsm
Sub No1_テストマクロ()
    ' 処理内容 
    ' 今回は適当に
    MsgBox "うんち"
End Sub
Sub No2_テストマクロ()
    ' 処理内容 
    MsgBox "ちんちn"
End Sub

アドイン

標準モジュールを追加

テストマクロアドイン.xlam
Public Sub テストマクロアドイン(control As IRibbonControl)
    Dim id
    id = control.id
    Call Application.Run("テストマクロ.xlsm!" & id)
End Sub

アドインファイルを分解しxmlを編集

こちらのサイトが画像付きでわかりやすい
https://resthill.blog/addin-design1/

アドインファイルの拡張子を手動でzipに変換

マクロアドイン.xlam => マクロアドイン.zip

image.png

↓ 警告出るけど無視

image.png

↓ 展開するとこんな感じ

image.png

↓展開後、カスタム用のフォルダ&カスタム用xmlを作成

image.png
image.png

customUI.xml・その他設定

Point

id = テストマクロのマクロ名
onAction = テストマクロアドイン

customUI.xml
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui">
  <ribbon>
    <tabs>
      <tab id="このidはなんでもいいよ" label="リボンに表示されちゃう">
        <group id="おてぃnてぃn" label="ここもリボンに表示さr">
            <button id="No1_テストマクロ" label="No1" size="large" onAction="テストマクロアドイン"/> 
            <button id="No2_テストマクロ" label="No2" size="large" onAction="テストマクロアドイン"/>  
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

_relsフォルダ内、.relsファイルを編集

Relationshipタグが並んでいるところに最後以下を追記

追記文
<Relationship Id="majinanndemoii" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml"/>
.rels(追記後)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
<Relationship Id="rId3" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/extended-properties" Target="docProps/app.xml"/>
<Relationship Id="rId2" Type="http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties" Target="docProps/core.xml"/>
<Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="xl/workbook.xml"/>
<Relationship Id="majinanndemoii" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml"/>
</Relationships>

あとは展開したファイルを再度圧縮(zip)→拡張子変更(xlam)

テストマクロ起動&アドインファイルを起動してリボンが追加され、ボタンで動くようになるはず

以上!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?