Home > Excel VBA | Useful Technic > Excel シートを順番に並び替えるマクロ(Rearranging Excel Sheets in order)

Excel シートを順番に並び替えるマクロ(Rearranging Excel Sheets in order)


エクセルシートを指定した順番に並びかえるマクロを紹介します。

Rearranging Excel sheets to prevent error.

エクセルシートを選択する際、Sheets(1).Activate と書いてますが、このコードは、 最初のタブにあるシートを選択します。"Sheet 1" から "Sheet 3" まで順番に並んでいる場合はSheets(1).Activateを実行すると、"Sheet 1" がアクティブになりますが、もし "Sheet 2"や"Sheet 3" が最初のタブに並び替えられている場合は、最初にあるシートがアクティブになります。

これを知らないでマクロを実行したとき、誤って間違ったシートを選択してしまうと重要なデータが消えてしまう恐れがあります。こういった予期せぬエラーを防ぐために、通常はSheets("Sheet 1")と名前を入れるようにするようにします。もし、多くのシートがあるエクセルファイルで繰り返しシートを選択するマクロを使用する場合は、シートを Sheets(番号) で選択しなければいけないことがあります。以下のコードはシートを指定した順番に並び替えるものです。Option Base 0 と書いてますが、これは配列の最初を0に設定するものです。もし、Option Base 1 を指定すれば、SheetArray(1)~SheetArray(5) と書き直す必要があります。下記に紹介するコードをマクロプログラムを実行する前に呼び出せば、指摘したエラーを回避することが出来ます。

When the code "Sheets(1).Activate" is excuted, Excel will activate the first sheet in Tab. Usually, you need to indicate the name of sheet to prevent error. Sometime it may happen that program select wrong sheet and delete cell's data. In case, you want to carry out the VBA program that select specific sheet repeatedly, it's necessary to write code as "Sheets(value of integer)". The source code below is to rearrange sheets in order. It may be useful if you call it before program run.

Comments:0

Comment Form

Home > Excel VBA | Useful Technic > Excel シートを順番に並び替えるマクロ(Rearranging Excel Sheets in order)

Return to page top