ExcelのSheets.Addメソッド | Microsoft Learn

この記事では、新しいワークシート、グラフ、またはマクロシートを作成する方法について説明しています。構文は「expression.Add(Before、After、Count、Type)」で、引数には新しいシートを追加する位置やシートの種類(xlWorksheet、xlChartなど)を指定できます。引数が省略された場合は、アクティブなシートの前に新しいシートが挿入されます。例として、最終ワークシートの前または後に新しいワークシートを挿入する方法が示されています。32ビットExcel 2010では、同時に255シート以上作成できません。

Excel VBAでワークシートを追加する方法

Excel VBA(Visual Basic for Applications)は、Microsoft Excelで利用できる強力なプログラム言語であり、ユーザーが自動化タスクを実行したり、複雑な計算を行ったりを可能にします。本記事では、Excel VBAを使用して新しいワークシートを作成する方法について詳しく解説します。

新しいワークシートの作成

新しいワークシート、チャート、またはマクロシートを作成するには、Sheets.Addメソッドを使用します。このメソッドを使用すると、新しいワークシートがアクティブなシートとして設定され、自動的に作成されます。

構文

expression.Add(Before, After, Count, Type)
  • expression: Sheetsオブジェクトを表す変数。
  • Before: 新しいシートを追加する前のシートを指定するオブジェクト(オプション)。
  • After: 新しいシートを追加する後のシートを指定するオブジェクト(オプション)。
  • Count: 追加するシートの数(オプション)。デフォルトは選択したシートの数です。
  • Type: シートの種類を指定します。以下の定数のいずれかを指定できます:xlWorksheetxlChartxlExcel4MacroSheet、またはxlExcel4IntlMacroSheet。既存のテンプレートに基づいてシートを挿入する場合は、テンプレートのパスを指定します。デフォルトはxlWorksheetです。

戻り値

戻り値は、新しいワークシート、チャート、またはマクロシートを表すオブジェクト値です。

注意事項

  • BeforeAfterの両方が省略された場合、新しいシートはアクティブなシートの前に挿入されます。

以下は、アクティブなワークブックの最後のワークシートの前に新しいワークシートを挿入する例です。

ActiveWorkbook.Sheets.Add Before:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)

次の例では、アクティブなワークブックの最後のワークシートの後に新しいワークシートを挿入し、返されたオブジェクト参照をローカル変数に格納します。

Dim sheet As Worksheet
Set sheet = ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))

注意

32ビットのExcel 2010では、このメソッドを使用して一度に255シート以上を作成することはできません。

サポートとフィードバック

Office VBAやこのドキュメントについて質問やフィードバックがある場合は、Office VBAのサポートとフィードバックをご覧ください。サポートを受けたり、フィードバックを提供したりするための手段について案内されています。


このようにして、Excel VBAを利用して新しいワークシートを簡単に追加できます。自動化の可能性を広げるために、VBAを活用してみてください!

————-

Sheets.Add method (Excel) | Microsoft Learn

Source link

The article provides information about using the Add method in Excel VBA to create a new worksheet, chart, or macro sheet within an active workbook. When invoked, the new sheet becomes the active sheet.

Syntax

The syntax for the Add method is:

expression.Add (Before, After, Count, Type)

Here, expression represents a Sheets object.

Parameters

  • Before (Optional): An object specifying the sheet before which the new sheet will be inserted.
  • After (Optional): An object specifying the sheet after which the new sheet will be inserted.
  • Count (Optional): The number of sheets to be added (defaulting to the number of selected sheets).
  • Type (Optional): Specifies the type of sheet to be created (like xlWorksheet, xlChart, etc.). The default is an Excel worksheet.

Return Value

The method returns an Object that represents the newly created sheet.

Remarks

If both Before and After are omitted, the new sheet is placed before the currently active sheet.

Examples

  1. To insert a new worksheet before the last sheet:

    ActiveWorkbook.Sheets.Add Before:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count)
  2. To insert a new worksheet after the last sheet and store the reference in a variable:
    Dim sheet As Worksheet
    Set sheet = ActiveWorkbook.Sheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))

Note

In Excel 2010 (32-bit version), there is a limitation of creating no more than 255 sheets at once.

For support or feedback regarding Office VBA, the article advises utilizing the Office VBA support channels.

関連記事