メインコンテンツへスキップ

ユーザーにマクロ対応のブックとして保存するように強制するにはどうすればよいですか?

Excelブックを保存すると、デフォルトでxlsxファイル形式で保存されます。複数のコードがある場合、このファイル形式ではブックからマクロコードが削除されます。 コードを保持するには、ブックをExcelマクロとして保存する必要があります-ブック形式を有効にします。 ユーザーにマクロ対応のブックとして保存させるにはどうすればよいですか?

ユーザーにVBAコードを使用してマクロ対応のブックとして保存するように強制する


矢印青い右バブル ユーザーにVBAコードを使用してマクロ対応のブックとして保存するように強制する

ブックをデフォルトでマクロ対応のブックとして保存するには、次のVBAコードが役立つ場合があります。次のようにしてください。

1。 を押し続けます Alt + F11 キーを押して アプリケーション向け Microsoft Visual Basic 窓。

2。 次にダブルクリックします このワークブックVBAプロジェクト セクションを開いて新しい空のモジュールを開き、次のコードをコピーしてモジュールに貼り付けます。

VBAコード:デフォルトでワークブックをマクロ対応のワークブックとして強制的に保存します。

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Updateby Extendoffice
Dim xFileName As String
If SaveAsUI <> False Then
    Cancel = True
    xFileName = Application.GetSaveAsFilename(, "Excel Macro-Enabled Workbook (*.xlsm), *.xlsm", , "Save As xlsm file")
    If xFileName <> "False" Then
      Application.EnableEvents = False
      ActiveWorkbook.SaveAs Filename:=xFileName, FileFormat:=xlOpenXMLWorkbookMacroEnabled
      Application.EnableEvents = True
    Else
      MsgBox "Action Cancelled"
      Cancel = True
      Exit Sub
    End If
End If
End Sub

doc 強制的に xlsm 1 として保存

3. 次に、このコードウィンドウを保存して閉じます。これ以降、ユーザーがこのワークブックとして保存すると、次のように保存されます。 Excelマクロが有効なブック 次のスクリーンショットに示すように、デフォルトでフォーマットします。

doc 強制的に xlsm 2 として保存

最高のオフィス生産性向上ツール

🤖 Kutools AI アシスタント: 以下に基づいてデータ分析に革命をもたらします。 インテリジェントな実行   |  コードを生成  |  カスタム数式の作成  |  データを分析してグラフを生成する  |  Kutools関数を呼び出す...
人気の機能: 重複を検索、強調表示、または識別する   |  空白行を削除する   |  データを失わずに列またはセルを結合する   |   数式なしのラウンド ...
スーパールックアップ: 複数の基準の VLookup    複数の値の VLookup  |   複数のシートにわたる VLookup   |   ファジールックアップ ....
詳細ドロップダウン リスト: ドロップダウンリストを素早く作成する   |  依存関係のドロップダウン リスト   |  複数選択のドロップダウンリスト ....
列マネージャー: 特定の数の列を追加する  |  列の移動  |  Toggle 非表示列の表示ステータス  |  範囲と列の比較 ...
注目の機能: グリッドフォーカス   |  デザインビュー   |   ビッグフォーミュラバー    ワークブックとシートマネージャー   |  リソースライブラリ (自動テキスト)   |  日付ピッカー   |  ワークシートを組み合わせる   |  セルの暗号化/復号化    リストごとにメールを送信する   |  スーパーフィルター   |   特殊フィルター (太字/斜体/取り消し線をフィルター...) ...
上位 15 のツールセット12 テキスト ツール (テキストを追加, 文字を削除する、...)   |   50+ チャート 種類 (ガントチャート、...)   |   40+ 実用的 (誕生日に基づいて年齢を計算する、...)   |   19 挿入 ツール (QRコードを挿入, パスから画像を挿入、...)   |   12 変換 ツール (数字から言葉へ, 通貨の換算、...)   |   7 マージ&スプリット ツール (高度な結合行, 分割セル、...)   |   ... もっと

Kutools for Excel で Excel スキルを強化し、これまでにない効率を体験してください。 Kutools for Excelは、生産性を向上させ、時間を節約するための300以上の高度な機能を提供します。  最も必要な機能を入手するにはここをクリックしてください...

説明


Officeタブは、タブ付きのインターフェイスをOfficeにもたらし、作​​業をはるかに簡単にします

  • Word、Excel、PowerPointでタブ付きの編集と読み取りを有効にする、パブリッシャー、アクセス、Visioおよびプロジェクト。
  • 新しいウィンドウではなく、同じウィンドウの新しいタブで複数のドキュメントを開いて作成します。
  • 生産性を 50% 向上させ、毎日何百回もマウス クリックを減らすことができます!
Comments (11)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
The VBA code forcing user to save in .xlsm file format works very well for me. I was looking for this solution for quite some time. Many thanks for the help!
This comment was minimized by the moderator on the site
Any way to have this VBA in a file that is a true template file type?
This comment was minimized by the moderator on the site
When you are ready to create the template, I believe you must use the Immediate Window to run
ActiveWorkbook.SaveAs Filename:="ENTER YOUR TEMPLATE NAME", FileFormat:=xlOpenXMLTemplateMacroEnabled
This comment was minimized by the moderator on the site
Any way to have this VBA in a template file
This comment was minimized by the moderator on the site
Keep getting a 1004 ERROR.
Using Off365 2016 for MAC. Got a solutions?
This comment was minimized by the moderator on the site
Hello, Jonathan,
This code works well in Windows Office, but it not tested in MAC.
You should search for a code for working in MAC.
Thank you!
This comment was minimized by the moderator on the site
fileformat: =52 is a nicer was as making macro enabled workbook. fileformat:=51 is normal workbook.
This comment was minimized by the moderator on the site
I'm using Excel 2016 for Mac, and when I use this VBA code to achieve the "force save as .xlsm" I get the following error:

Run-time error '1004':

Method 'GetSaveAsFilename' of object '_Application' failed

Any guidance someone could provide around this error and how I may need to adjust the code to work on the Mac version of Excel would be greatly appreciated!
This comment was minimized by the moderator on the site
Nice one!

Saved me a lot of headache! :)
This comment was minimized by the moderator on the site
Saved me a lot of pain - thanks so much
This comment was minimized by the moderator on the site
This was so useful and worked like a charm thank you :)
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations