一般的な問題への対処
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
マクロ コンバーターを使用して VBA コードを Apps Script に変換する際に発生する可能性のある一般的な問題を以下に示します。
印刷
ファイルを印刷する VBA API は自動的に Apps Script に変換されますが、元の VBA API とは異なる動作をする可能性があります。以下に 2 つの例を示します。
VBA API | Apps Script での動作 |
---|
PrintOut | Apps Script に変換されますが、Apps Script API はプリンタではなくファイルに出力します。PDF ファイルを手動で印刷できます。 |
PrintToFile | Apps Script に変換します。PDF ファイルはマイドライブ フォルダに保存されます。 |
未変換のアイテム
次の機能はマクロ コンバータで変換されないため、手動で変換する必要があります。
トリガー
キーボード ショートカットと一部のイベントベースのトリガーという 2 種類のトリガーは、マクロ コンバータで変換されません。多くの場合、これらのトリガーは手動で作成できます。
キーボード ショートカット
キーボード ショートカットを追加するには、関数をマクロとしてインポートする手順に沿って操作します。
イベントに基づくトリガー
VBA コードのイベント(BeforeClose
や BeforeSave
など)には、Apps Script に同等のものがないものもありますが、回避策を作成できる場合があります。
BeforeClose
などのイベントでは、スプレッドシートを閉じる前に実行する必要があるアクションを行うためのカスタム メニューやボタンを作成できます。
Google スプレッドシートでは編集内容が自動的に保存されるため、BeforeSave
などのイベントの回避策は実現できません。
VBA では、UserForm はアプリケーションのユーザー インターフェース(UI)のウィンドウまたはダイアログ ボックスです。Macro Converter では UserForm は変換されません。Apps Script で手動で作成できます。
- パソコンで、変換したファイルを Google スプレッドシートで開きます。
- 上部の [拡張機能] > [** Apps Script**] をクリックします。
- エディタの左側にある [ファイル] の横にある [ファイルを追加] add > [HTML] をクリックします。HTML ファイルには、元の VBA UserForm と同じ名前を付けることをおすすめします。
- フォームに表示するフィールドと情報を追加します。HTML フォームについて詳しくは、W3school.com をご覧ください。
- 左側で、変換されたコードを含む Apps Script ファイル(GS ファイル)をクリックします。
- コードに
onOpen()
トリガーがすでに存在する場合は、以下のコードで更新します。コードに onOpen()
トリガーがない場合は、次のコードを追加します。
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('User Form')
.addItem('Show Form', 'showForm')
.addToUi();
}
function showForm() {
var html = HtmlService.createHtmlOutputFromFile('userform_module_name')
.setWidth(100)
.setTitle('Sign-up for Email Updates');
SpreadsheetApp.getUi().showSidebar(html);
}
userform_module_name
は、追加した HTML ファイルの名前に置き換えます。
- 上部の [プロジェクトを保存] アイコン save をクリックします。
- Google スプレッドシートに切り替えて、ページを再読み込みします。
- Google スプレッドシートの上部にある [User Form] > [Show Form] をクリックします。
名前付き範囲
Excel では、名前付き範囲は単一のセルまたはセルの範囲に付けられた名前です。
Excel ファイルを Google スプレッドシートに変換する際、一部の名前付き範囲はサポートされていないため、変換されません。以下に 2 つの例を示します。
サポートされていない名前付き範囲 | 説明 |
---|
テーブル | Google スプレッドシートではサポートされていませんが、回避策があります。
この名前付き範囲をスプレッドシートで再作成するには、テーブル範囲の A1 表記を指す名前付き範囲を追加します。変換されたコードが認識できるように、VBA コードで元の名前付き範囲と同じ名前を使用します。 |
範囲のリスト | Google スプレッドシートではサポートされていません。回避策はありません。 |
関連記事
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-31 UTC。
[null,null,["最終更新日 2025-08-31 UTC。"],[[["\u003cp\u003eWhen converting VBA to Apps Script, printing functions behave differently; \u003ccode\u003ePrintOut\u003c/code\u003e prints to a file instead of directly to a printer, while \u003ccode\u003ePrintToFile\u003c/code\u003e saves the PDF to your MyDrive.\u003c/p\u003e\n"],["\u003cp\u003eThe Macro Converter does not handle certain VBA features, including some triggers, UserForms, and unsupported named ranges, requiring manual conversion.\u003c/p\u003e\n"],["\u003cp\u003eKeyboard shortcuts and some event-based triggers (like \u003ccode\u003eBeforeClose\u003c/code\u003e or \u003ccode\u003eBeforeSave\u003c/code\u003e) from VBA require manual configuration or workarounds in Apps Script due to functional differences.\u003c/p\u003e\n"],["\u003cp\u003eUserForms need to be recreated in Apps Script using HTML, and integrated into the spreadsheet through custom menus or sidebars.\u003c/p\u003e\n"],["\u003cp\u003eWhile most named ranges are converted, tables and lists of ranges require manual adjustments or are unsupported, necessitating alternative approaches in Google Sheets.\u003c/p\u003e\n"]]],[],null,["# Address common issues\n\nCommon issues you might run into when you convert your VBA code to Apps Script\nwith the Macro Converter are listed below.\n\nPrinting\n--------\n\nVBA APIs that print files are automatically converted to Apps Script, but might\nbehave differently than the original VBA API. Below are two examples:\n\n| VBA API | Behavior in Apps Script |\n|---------------|------------------------------------------------------------------------------------------------------------------------------|\n| `PrintOut` | Converts to Apps Script, but the Apps Script API prints to a file instead of a printer. You can manually print the PDF file. |\n| `PrintToFile` | Converts to Apps Script. The PDF file is saved in your MyDrive folder. |\n\nUnconverted items\n-----------------\n\nThe following features aren't converted by the Macro Converter and need to be\nconverted manually:\n\n- Some types of [Triggers](#triggers)\n- [Userforms](#userforms)\n- Unsupported types of [Named ranges](#named_ranges)\n\n### Triggers\n\nTwo types of triggers, keyboard shortcuts and some event-based triggers, aren't\nconverted by the Macro Converter. In many cases, you can create these triggers\nmanually.\n\n#### Keyboard shortcuts\n\nTo add keyboard shortcuts, follow the steps to [import functions as\nmacros](https://developers.google.com/apps-script/guides/sheets/macros#importing_functions_as_macros).\n\n#### Event-based triggers\n\nSome events from VBA code, like `BeforeClose` or `BeforeSave`, don't have\nequivalents in Apps Script, but you might be able to create a workaround.\n\nFor events like `BeforeClose`, you can create a custom menu or button to click\nto perform the action that needs to take place before you close the spreadsheet.\n\nSince Google Sheets auto-saves each edit, workarounds for events like\n`BeforeSave` aren't feasible.\n\n### Userforms\n\nIn VBA, a [*UserForm*](https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/userform-object) is a window or dialog box in an application's user\ninterface (UI). The Macro Converter doesn't convert UserForms. You can manually\ncreate them in Apps Script.\n\n#### Create a user form dialogue\n\n1. On your computer, open the converted file in [Google Sheets](http://sheets.google.com).\n2. At the top, click **Extensions** \\\u003e \\*\\* Apps Script\\*\\*.\n3. At the left of the editor next to \"Files,\" click Add a file add **\\\u003e HTML**. We recommend that you give the HTML file the same name as your original VBA UserForm.\n4. Add the fields and information you want to appear in your form. Learn more about HTML forms at [W3school.com](https://www.w3schools.com/html/html_forms.asp).\n5. At the left, click the Apps Script file (GS file) that has your converted code.\n6. If you already have an `onOpen()` trigger in your code, update it with the code below. If you don't have the `onOpen()` trigger in your code, add the code below. \n\n ```gdscript\n function onOpen() {\n SpreadsheetApp.getUi()\n .createMenu('User Form')\n .addItem('Show Form', 'showForm')\n .addToUi();\n }\n function showForm() {\n var html = HtmlService.createHtmlOutputFromFile('userform_module_name')\n .setWidth(100)\n .setTitle('Sign-up for Email Updates');\n SpreadsheetApp.getUi().showSidebar(html);\n }\n \n ```\n7. Replace `userform_module_name` with the name of the HTML file you added.\n8. At the top, click Save project save.\n9. Switch to the Google Sheet and reload the page.\n10. At the top of the Google Sheet, click **User Form** \\\u003e **Show Form**.\n\n### Named ranges\n\nIn Excel, named ranges are names given to a single cell or range of cells.\n\nWhen you convert your Excel file to Google Sheets, some types of named ranges\nwon't be converted because they're not supported. Below are two examples:\n\n| Unsupported named ranges | Description |\n|--------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| Tables | Not supported in Google Sheets, but has a workaround. \u003cbr /\u003e To recreate this named range in Sheets, add a named range that points to the A1 notation of the table range. Use the same name as the original named range in your VBA code so that the converted code recognizes it. |\n| List of ranges | Not supported in Google Sheets. There isn't a workaround. |\n\nRelated articles\n----------------\n\n- [Macro Converter add-on overview](/apps-script/guides/macro-converter/overview)\n- [Determine if VBA macros are compatible](/apps-script/guides/macro-converter/compatibility-report)\n- [Convert VBA macros to Apps Script](/apps-script/guides/macro-converter/convert-files)\n- [Fix errors in your converted code](/apps-script/guides/macro-converter/fix-conversion-errors)\n- [Watch Macro Converter tutorials](/apps-script/guides/macro-converter/tutorials)\n- [List of compatible VBA APIs](/apps-script/guides/macro-converter/compatible-vba-apis)"]]