Excel-VBA MSGBOX、INPUTBOX

Excel Macroを多用するなとの記事を、時々見かける。
内容は大抵、動作はするが、中身がわからないとのことだ。

本当にそうだろうか、これは投稿者は実は孫引きしているだけで、VBAをまともに
記述出来ないのだろうと思っている。

Macroは作りっぱなしでは、実に可読性が悪い、これは事実。
しかし、これにコメントを入れ、MSGBOXやINPUTBOXを少し書き足してやれば、
安全度は格段に向上する。

以下は、その実例、Macroは最後のブロックのみ、あとはMSGBOXで使用者が判断
できるようになっている。
※複数のMSGBOXを使う場合、処理中断はGotoで終了させるが、Gotoは多用してはダメ。


Sub Pre_Print()
'
' Pre_Print Macro



'
Dim St_c As String, En_c As String

Dim St_n As Long, En_n As Long

Dim rc As Integer



St_c = InputBox("開始行を入力してください", "値をクリアします", "14")

St_n = Val(St_c)

If St_n < 10 Then

MsgBox ("値が小さすぎます")

GoTo Continue
End If


En_c = InputBox("終了行を入力してください", "値をクリアします", "25")


En_n = Val(En_c)

If En_n > 59 Then

MsgBox ("値が大きすぎます")

GoTo Continue
End If



rc = MsgBox(St_c & " - " & En_c & "をクリアします", vbYesNo + vbQuestion, "確認")

If rc = vbYes Then

MsgBox "処理を行います"

Else

MsgBox "処理を中断します"

End If


'
'-------------------------------------------------------------------
Sheets("SKD1").Select

Range(Cells(St_n, 3), Cells(En_n, 33)).Select

Selection.ClearContents
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
ActiveWindow.SmallScroll Down:=-6
Range("A1").Select

'---------------------------------------------------------------

Continue:

End Sub



わかお かずまさ
VegaSystems

📷📷📷📷📷📷
#LAN_PRO
#Bloguru
#kuma

People Who Wowed This Post

×
  • If you are a bloguru member, please login.
    Login
  • If you are not a bloguru member, you may request a free account here:
    Request Account
This user only allows bloguru members to make comments.
If you are a bloguru member, please login.
Login
If you are not a bloguru member, you may request a free account here:
Request Account