Excelでランチャーソフトを作ってみよう

Step 3 入力結果の保存と反映



Homeに戻る > Excelでアプリケーションソフト > ランチャーソフト

シートイベント

下記のVBAコードに変更してください。

Option Explicit

'コマンドボタン クリックイベント
Private Sub CommandButton1_MouseDown(ByVal Button As Integer, _
ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)     '右クリックの場合
    If Button = 2 Then
        ButtonNo = 1
        ButtonCaption = CommandButton1.Caption
        'ファイル指定フォームを開く
        UserForm1.Show
        '見出しをコマンドボタンに表示
        CommandButton1.Caption = ButtonCaption
    End If
End Sub

ユーザーフォームイベント

下記のVBAコードに変更してください。

'OKボタン
Private Sub CommandButton3_Click()
    Dim f As Double
    
    '行の高さを保存
    f = Range("AA" & ButtonNo).RowHeight
    '入力値を保存
    Range("AA" & ButtonNo) = TextBox1.Text
    Range("AB" & ButtonNo) = TextBox2.Text
    '行の高さを戻す
    Range("AA" & ButtonNo).RowHeight = f
    '見出しを保存
    ButtonCaption = TextBox1.Text
    '閉じる
    Unload Me
End Sub

Private Sub UserForm_Initialize()
    'ボタンに対応するデータをテキストボックスにコピー
    TextBox1 = Range("AA" & ButtonNo)
    TextBox2 = Range("AB" & ButtonNo)
    'カーソルを先頭にする
    TextBox1.SelStart = 0
    TextBox1.SelLength = 0
    TextBox2.SelStart = 0
    TextBox2.SelLength = 0
End Sub

下記のVBAコードを追加してください。
'キャンセルボタン
Private Sub CommandButton2_Click()
    '閉じる
    Unload Me
End Sub



シートとユーザーフォーム

開く時、コマンドボタンのCaptionと、指定ファイルがテキストボックスに反映されます。
「OK」ボタンで入力値がセルに保存され、コマンドボタンに表示されます。


Homeに戻る > Excelでアプリケーションソフト > ランチャーソフト

■■■
このサイトの内容を利用して発生した、いかなる問題にも一切責任は負いませんのでご了承下さい
■■■
当ホームページに掲載されているあらゆる内容の無許可転載・転用を禁止します


Copyright (c) Excel-Excel ! All rights reserved