0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[備忘録] Excelマクロ チェックボックス(ActiveX コントロール)で指定したシートを初期化

Posted at

◇Excelマクロ チェックボックス(ActiveX コントロール)で指定したシートをクリアで初期化する

〇参考

 ↓基本処理
 ・Excel マクロ チェックボックス(activateX コントロール)を使用した処理分岐
  https://qiita.com/ema_gencyCall/items/55fce401436134bd2555

〇事前準備

 ・初期化対象のシートを準備
  今回は以下のようなフォーマットを想定。各シートの2行目移行をクリアする。
jizen3.PNG
 
 ・初期化対象を選ぶメインシートはこんな感じ。
jizen4.PNG

〇ソース

Option Explicit

'シート名
Const SHEET_NAME_MAIN = "sheet1"
Const SHEET_NAME_K = "k"
Const SHEET_NAME_Y = "y"
Const SHEET_NAME_S = "s"
Const SHEET_NAME_M1 = "m1"
Const SHEET_NAME_M2 = "m2"

Sub selectSheetProcess()

Dim rc As Integer

rc = MsgBox("初期化処理開始しますか?", vbOKCancel)

If rc = vbCancel Then

MsgBox ("処理中断。")
Exit Sub

End If

'mainシートをactive
Worksheets(SHEET_NAME_MAIN).Activate

'以下チェックボックスの状態で処理分岐
'チェックが入っていたらシート初期化処理を呼び出す

'sheet:k
If ActiveSheet.checkBox_k.Value = True Then
Call sheetClear(SHEET_NAME_K)
End If

'sheet:y
If ActiveSheet.checkBox_y.Value = True Then
Call sheetClear(SHEET_NAME_Y)
End If

'sheet:s
If ActiveSheet.checkBox_s.Value = True Then
Call sheetClear(SHEET_NAME_S)
End If

'sheet:m1
If ActiveSheet.checkBox_m1.Value = True Then
Call sheetClear(SHEET_NAME_M1)
End If

'sheet:m2
If ActiveSheet.checkBox_m2.Value = True Then
Call sheetClear(SHEET_NAME_M2)
End If

MsgBox ("処理終了。")

End Sub

'シート初期化処理
Sub sheetClear(sheetName As String)

Dim clearRowRange As String

'クリア範囲を2~シート最大行に設定
clearRowRange = "2:" & Rows.Count

'指定シートの2行目以降の値をクリア
'クリア対象:値と数式
Worksheets(sheetName).Range(clearRowRange).ClearContents

End Sub

〇結果
チェックを入れたシートが初期化される!

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?