VBA名前変更シート| VBAコードを使用してExcelワークシートの名前を変更するにはどうすればよいですか?

Excelでシートの名前を変更するには、ワークシートが表示されている下のタスクバーからダブルクリックして行いますが、VBAではSheetsまたはWorksheetプロパティメソッドを使用してシートの名前を変更します。VBAでシートの名前を変更する構文は次のとおりです。古いシート名」)。Name=「新しいシート名」。

ExcelVBAでシートの名前を変更する

私たちは皆、自分の身元や都合に合わせてワークシートの名前を変更するというこのタスクを実行しましたね。名前の変更は習得するロケット科学ではありませんが、VBAコーダーの場合は、ワークシートの名前を変更するこのタスクを知っている必要があります。ワークシートの名前を使用して作業するため、VBAコーディングにおけるワークシート名の重要性を知ることが重要です。この記事では、ExcelVBAコーディングを使用してシートの名前を変更する方法を示します。

VBAでシートの名前を変更するにはどうすればよいですか?

ワークシートの名前を変更するのに特別なスキルは必要ありません。既存のシート名を入力して、変更するシート名を参照するだけです。

たとえば、「シート1」という名前のシートの名前を変更する場合は、Worksheetオブジェクトを使用してその名前でシートを呼び出す必要があります。

ワークシート(「シート1」)

シート名を指定した後、「Name」プロパティを選択してワークシート名の名前を変更する必要があります。

ワークシート(「シート1」)。名前

次に、Nameプロパティを希望どおりの名前に設定する必要があります。

Worksheets(“ Sheet1”)。Name =“ New Name”

このように、Nameプロパティを使用してVBAのワークシート名の名前を変更できます。

記事の次のセクションでは、ワークシートを変更または名前変更する例をどんどん紹介します。

ExcelVBAでワークシートの名前を変更する例

以下は、VBA名前変更シートの例です。

このVBA名前変更シートテンプレートはここからダウンロードできます–VBA名前変更シートテンプレート

例1-VBA変数を使用してシートを変更または名前変更します。

例については、以下のサンプルコードをご覧ください。

コード:

 Sub Rename_Example1()Dim Ws As Worksheet Set Ws = Worksheets( "Sheet1")Ws.Name = "New Sheet" End Sub 

上記のコードでは、最初に変数をワークシートとして宣言しました。

 ワークシートとして薄暗い

次に、worksheetsオブジェクトを使用して、変数への参照を「Sheet1」として設定しました。

 Ws = Worksheets( "Sheet1")を設定します

これで、変数「Ws」はワークシート「Sheet1」の参照を保持します。

「Ws」変数を使用して、ワークシートの名前を「NewSheet」に変更しました。

このコードは、「Sheet1」の名前を「NewSheet」に変更します。

コードを手動で実行するか、ショートカットキーF5を使用して実行すると、下付き文字の範囲外エラーが発生します。

前のステップ自体で「Sheet1」という名前のワークシートを「新しいシート」にすでに変更しているため、このエラーが発生する理由。ワークシート名「Sheet1」が使用できなくなったため、VBAはこのエラーをスローします。

例2–すべてのワークシート名を1つのシートで取得します。

ブックのすべてのワークシート名を1枚のシートで取得できます。以下のコードは、すべてのワークシート名を抽出します。

コード:

 Sub Renmae_Example2()Dim Ws As Worksheet Dim LR As Long For Each Ws In ActiveWorkbook.Worksheets LR = Worksheets( "Main Sheet")。Cells(Rows.Count、1).End(xlUp).Row + 1 Cells(LR、 1).ActiveCell.Value = Ws.Name Next Ws EndSubを選択します 

このコードは、使用可能なすべてのワークシート名を「メインシート」という名前のシートに抽出します。

例3–VBAを使用して永続的な名前をExcelワークシートに設定する

Since we work with sheet names in coding it is important to set permanent names to them. How do we set permanent names for them?

For an example look at the below code.

Code:

 Sub Rename_Example3() Worksheets("Sheet1").Select End Sub 

The above code will select the Sheet1.

If your workbook is used by many people, if someone changed the name of the worksheet then we will get the Subscript Out of Range error.

To avoid this we can set the permanent name to it. To set the permanent name to follow the below steps.

Step 1: Select the sheet we need to set the permanent name to in Visual Basic Editor.

Step 2: Press F4 key to see the Properties window.

Step 3: Under Name, Property Change the name to “New Name”.

As you can see one name is showing as “Sheet1” and in a bracket, we can see the new name as “New Sheet”.

Now in coding, we will use the new name instead of an actual visible name.

Code:

 Sub Rename_Example3() NewSheet.Select End Sub 

Now come back to the worksheet window, we can still see the sheet name as “Sheet1” only.

Now I will change the sheet name to “Sales”.

If I run the code using the F5 key or manually then it will still select the sheet named “Sales” only. Since we had given a permanent name to it, still it will select the same sheet only.