ExcelVBAへの道
|
Sub エラー処理() On Error GoTo syori1 ----------処理------------------ syori1: MsgBox"エラー発生" End Sub --------------------------------------------------------------------------------- Sub エラー無視() On Error Resume Next 'エラーが発生しても無視して次の行を実行 ----------処理------------------ End Sub --------------------------------------------------------------------------------- Sub アラートを出さない() Application.DisplayAlerts = False End Sub --------------------------------------------------------------------------------- Sub 10秒後ごとにサブプロシージャを切替実行() Application.OnTime Now + TimeValue("00:00:10"),"2next" End Sub 'サブプロシージャ Sub 2next() Application.OnTime Now + TimeValue("00:00:10"),"3next" End Sub 'サブプロシージャ Sub 3next() Application.OnTime Now + TimeValue("00:00:10"),"4next" End Sub 'サブプロシージャ Sub 4next() Application.OnTime Now + TimeValue("00:00:10"),"5next" End Sub --------------------------------------------------------------------------------------- Sub 別のプロシージャを実行() ----------処理------------------ call betu1 End Sub Sub betu1() ----------処理------------------ End Sub -------------------------------------------------------------------------------------- Sub 予定時刻に実行() 'Application.OnTime TimeValue("12:00:00"),"sound" '12時に実行 Application.OnTime Now + TimeValue("00:00:10") ,"sound" '10秒後に実行 End Sub Sub sound() Dim i As integer for i = 1 to 10 Beep MsgBox"実行" Next i End Sub ----------------------------------------------------------------------------------- Private Sub Label1_Click() UserForm1.Label1.Height = 120 UserForm1.Label1.Width = 200 End Sub ------------------------------------------------------------------------------------- VBAでダイアログボックスを作成できる。 プロシージャボックスで”クリック””ダブルクリック”などのイベント動作を設定する 以下ダイアログボックスのコード ------------------------------------------------- Private Sub Label1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) Dim mylabel As Control Set mylabel = UserForm1.Label1 mylabel.BackColor = RGB(255, 0, 0) End Sub ------------------------------------------------------------------------------- Private Sub Label1_Click() UserForm1.Label1.Height = 120 UserForm1.Label1.Width = 200 End Sub --------------------------------------------------------------------------------- Private Sub Label1_DblClick(ByVal Cancel As MSForms.ReturnBoolean) Dim mylabel As Control Set mylabel = UserForm1.Label1 mylabel.BackColor = RGB(255, 0, 0) End Sub -------------------------------------------------------------------------------- Private Sub Label1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single) UserForm1.Label1.ForeColor = RGB(255, 0, 0) End Sub -------------------------------------------------------------------------------- Private Sub UserForm_Activate() UserForm1.CommandButton1.ControlTipText = "表示・非表示ボタンです。" End Sub -------------------------------------------------------------------------------- Private Sub ボタン2_Click() Dim mybottn2 As Control Set mybottn2 = UserForm1.Label1 If mybottn2.Visible = True Then mybottn2.Visible = False Else mybottn2.Visible = True End If End Sub --------------------------------------------------------------------------------- Private Sub CommandButton2_Click() UserForm1.TextBox1.IMEMode = fmIMEModeOn '日本語入力オン UserForm1.TextBox1.SetFocus End Sub -------------------------------------------------------------------------------- Private Sub CommandButton3_Click() UserForm1.TextBox1.IMEMode = fmIMEModeOff '日本語入力オフ UserForm1.TextBox1.SetFocus End Sub -------------------------------------------------------------------------------- Private Sub UserForm_Click() UserForm1.TextBox2.PasswordChar = "*" 'パスワード用テキストボックス End Sub ----------------------------------------------------------------------------- Private Sub UserForm_Activate() UserForm1.ListBox1.RowSource = "h1:h10" 'リストボックスにデータ読み込み UserForm1.ComboBox1.RowSource = "j1:j10" 'コンボボックスにデータ読み込み End Sub ----------------------------------------------------------------------------- Private Sub CommandButton4_Click() If UserForm1.CheckBox1.Value = True Then With ActiveWindow.RangeSelection .Font.Name = "HGP行書体" .Font.Color = RGB(0, 0, 255) End With End If If UserForm1.CheckBox2.Value = True Then ActiveWindow.Zoom = 200 End If If UserForm1.CheckBox3.Value = True Then With ActiveWindow.RangeSelection .Font.Name = "MS ゴシック" .Font.Color = RGB(255, 0, 0) End With End If End Sub ------------------------------------------------------------------------------- Private Sub ToggleButton1_Click() 'トグルボタンで切替 If UserForm1.ToggleButton1.Value = True Then With ActiveWindow.RangeSelection .Font.Name = "HGP行書体" .Font.Color = RGB(0, 0, 255) End With Else With ActiveWindow.RangeSelection .Font.Name = "MS ゴシック" .Font.Color = RGB(255, 0, 0) End With End If End Sub |