エクセルのVBAについての質問

このQ&Aのポイント
  • エクセルのVBAを使用して、ボタンの選択に応じてテキストボックスの値をセルに反映させる方法を教えてください。
  • 具体的には、button1, button2, button3とテキストボックス1を関連付け、button4, button5, button6とテキストボックス2を関連付けたいと思っています。
  • また、button1とbutton4, button2とbutton5, button3とbutton6が同時に選択された場合のみ、テキストボックスの値をセルに反映させたいです。
回答を見る
  • ベストアンサー

エクセル

TEXTBOX1 button1 button2 button3 TEXTBOX2 button4 button5 button6 comanndobuton1 Private Sub CommandButton1_Click() Dim cnt As Long cnt = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row If Me.OptionButton1 = True Then Cells(cnt, 1) = Me.TextBox1 Else Cells(cnt, 1) = "" If Me.OptionButton4 = True Then Cells(cnt + 1, 1) = Me.TextBox2 Else Cells(cnt + 1, 1) = "" End If End If If Me.OptionButton2 = True Then Cells(cnt, 2) = Me.TextBox1 Else Cells(cnt, 2) = "" If Me.OptionButton5 = True Then Cells(cnt + 1, 2) = Me.TextBox2 Else Cells(cnt + 1, 2) = "" End If End If If Me.OptionButton3 = True Then Cells(cnt, 3) = Me.TextBox1 Else Cells(cnt, 3) = "" If Me.OptionButton6 = True Then Cells(cnt + 1, 3) = Me.TextBox2 Else Cells(cnt + 1, 3) = "" End If End If End Sub これで実行するとbutton1がtrueでありbutton4がtrueのように    bu2とbu5 bu3とbu6が一緒の時だけうまく以下なのですが ご指摘いただけないでしょうか

質問者が選んだベストアンサー

  • ベストアンサー
  • MT765
  • ベストアンサー率56% (1898/3332)
回答No.1

End Ifの位置を変えてみたらどうでしょうか If Me.OptionButton1 = True Then Cells(cnt, 1) = Me.TextBox1 Else Cells(cnt, 1) = "" End If If Me.OptionButton4 = True Then Cells(cnt + 1, 1) = Me.TextBox2 Else Cells(cnt + 1, 1) = "" End If

HAWKS123
質問者

お礼

有難うございました end ifの使い方でかわるんですね まだまだ勉強です

関連するQ&A

  • エクセル VBA OptionButtonからTextBox

    すいません! OptionButtonなら 下記の記述でエラー表示を 簡単にできるのですが これがOptionButtonではなく TextBoxならどのように変化したら 良いのでしょうか? すいません、教えて下さい! Private Sub 記録_Click() Dim i As Integer Dim Cnt As Integer Cnt = 0 For i = 1 To 6 Step 1 If Me.Controls("OptionButton" & i).Value Then Cnt = i Exit For End If Next i If Cnt = 0 Then MsgBox "選択されていません" Exit Sub End If If Me.Controls("Combobox" & Cnt).Value = "" Then MsgBox Me.Controls("OptionButton" & Cnt).Caption & " の内容が選択されていません" Exit Sub End If With 記入フォーム .TextBox5.Value = Me.Controls("OptionButton" & Cnt).Caption .TextBox6.Value = Me.Controls("Combobox" & Cnt).Value End With Unload Me End Sub

  • 12個のCommandbuttonをスマートに

    はじめまして 2003 XP UserFormにCommandbuttonが12個あります。 Private Sub CommandButton1_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "1" Else: TextBox1.Value = TextBox1 & "1" End If End Sub Private Sub CommandButton2_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "2" Else: TextBox1.Value = TextBox1 & "2" End If End Sub Private Sub CommandButton3_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "3" Else: TextBox1.Value = TextBox1 & "3" End If End Sub Private Sub CommandButton4_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "4" Else: TextBox1.Value = TextBox1 & "4" End If End Sub Private Sub CommandButton5_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "5" Else: TextBox1.Value = TextBox1 & "5" End If End Sub Private Sub CommandButton6_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "6" Else: TextBox1.Value = TextBox1 & "6" End If End Sub Private Sub CommandButton7_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "7" Else: TextBox1.Value = TextBox1 & "7" End If End Sub Private Sub CommandButton8_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "8" Else: TextBox1.Value = TextBox1 & "8" End If End Sub Private Sub CommandButton9_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "9" Else: TextBox1.Value = TextBox1 & "9" End If End Sub Private Sub CommandButton10_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "0" Else: TextBox1.Value = TextBox1 & "0" End If End Sub Private Sub CommandButton11_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "00" Else: TextBox1.Value = TextBox1 & "00" End If End Sub Private Sub CommandButton12_Click() If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & "000" Else: TextBox1.Value = TextBox1 & "000" End If End Sub  これをもっとスマートに出来ないでしょうか? 宜しくお願いします。

  • エクセル IF について!

    UserForm上にTextBoxとコマンドボタンがあり、TextBoxに数字を入れコマンドボタンをクリックすると'A.xlsをセットしてAシートの使用行を格納し検索して他のTextBoxにも反映させていくやり方でマクロを記述しています。そこでTextBoxに入力した数字がない場合はMsgBox”この数字はありません”という形にしたいのですが・・・どのようにすれば良いのか教えて下さい。 If Me.Controls("TextBox1" & Cnt).Value = "" Then MsgBox "呼出したい数字を入力して下さい" Exit Sub End If Set wbMyBook = Workbooks(ThisWorkbook.Name) If MsgBox("以前の記録を呼び戻しますか?", vbOKCancel) = vbOK Then Application.ScreenUpdating = False strMyBookPath = ThisWorkbook.Path If Dir(strMyBookPath & "\" & k1Name) <> "" Then 'あった場合そのブックが空いているか確認する。 flag = False For Each wb In Workbooks '開いていればTrue,開いていなければFalseを設定 If wb.Name = k1Name Then flag = True Exit For End If Next wb 'ブックが開いていなかった場合、ブックを開ける。 If flag = False Then Workbooks.Open strMyBookPath & "\" & k1Name End If Set k1 = Workbooks(k1Name) Set SH1 = k1.Worksheets("Sheet1") Else MsgBox WDName & "が存在していません。設置してください。", vbExclamation, "確認してください" Exit Sub End If lngYcnt_K = SH1.UsedRange.Rows.Count flag = False For lng = 1 To lngYcnt_K If CStr(TextBox1.Text) = CStr(SH1.Cells(lng, 1)) Then flag = True lngNumber = lng Exit For End If Next lng If flag = True Then TextBox3.Value = SH1.Cells(lngNumber, 2) '氏名 End If If SH1.Cells(lngNumber, 3) = "男" Then OptionButton1.Value = True ElseIf SH1.Cells(lngNumber, 3) = "女" Then OptionButton2.Value = True Else OptionButton1.Value = True OptionButton2.Value = False End If MsgBox " 記録を呼び戻しました" Else MsgBox"確認必要"⇒ここにもし数字が違っていたら表示させたいのですが・・・ End If MsgBox " 以前に記録しましたか?" Application.DisplayAlerts = False k1.Close saveChanges:=True Application.DisplayAlerts = True '-------------------------------------------------------------------------- '画面更新ON Application.ScreenUpdating = False End Sub

  • エクセル マクロ フォームにて

    エクセル マクロの記述教えて下さい! 1)エクセル Sheet1にコマンドボタンをクリックにてフォームが表示 2)UserForm1にコマンドボタンクリックにてUserForm2が表示 3)UserForm2に   OptionButton1(1、血圧)   OptionButton2(2、体温)   OptionButton3(3、脈)  と選択させてTextBox1に(1又は2・3)のみの数字番号だけ  表示させたい!  1の数字の場合は赤文字、2なら青文字にしたい! 4)UserForm2にコマンドボタンをクリックするとUserForm1の  TextBoxに数字(1~3)をそのまま表示させたい! すいません!分かりづらいですか?・・・今の段階での記述がこれです!が、下記の場合だと3)が文字(1、血圧)しか出来ません。 これを数字に認識させたいです! Private Sub CommandButton1_Click() Dim i As Integer Dim Cnt As Integer Cnt = 0 For i = 1 To 3 Step 1 If Me.Controls("OptionButton" & i).Value Then Cnt = i Exit For End If Next i If Cnt = 0 Then MsgBox "ひとつも選択されていません" Exit Sub End If With UserForm1 .TextBox1.Value = Me.Controls("OptionButton" & Cnt).Caption End With Unload Me End Sub 色々追加記述が必要ですが、頭が混乱しています! 是非、教えて下さい!

  • エクセル OptionButton

    FromにOptionButtonを貼り付けてセルの値を呼び出してFromのTextBoxなどに表示させるものを作成して上手くいっていましたが・・・・・ OptionButton1をクリックしたらSheetのセルに”男”と記載される マクロを作成しました。(以下の通りです) If オプション事故 = True Then SH2.Cells(lngNumber, 6) = "男" ElseIf OptionButton1 = True Then SH2.Cells(lngNumber, 6) = "男" ElseIf OptionButton2 = True Then SH2.Cells(lngNumber, 6) = "女" End If 今回困っているのはこれの逆で、セルの値が”男”なら OptionButton1にマークが付くようにしたいのですが こんなことできますか?下記のようにしてみましたができません! If SH1.Cells(lngNumber, 6) = "男" Then OptionButton1.Value = True ElseIf SH1.Cells(lngNumber, 6) = "女" Then OptionButton2.Value = True End If すいません教えて下さい!!

  • ユーザーフォームのデータ

    ユーザーファームを2つ作成しました。 そのユーザーフォームのデータを表の最終行に追加をしたいのです。 Range("A65536").End(xlUp).Offset(1,0).select を使おうと思っていますが、うまくいきません。 どなたか教えてください。 <ユーザーフォーム1> Private Sub CommandButton1_Click() Sheet2.Range("H7") = TextBox1 Sheet2.Range("I7") = TextBox2 Sheet2.Range("J7") = TextBox3 Sheet2.Range("K7") = TextBox4 Sheet2.Range("L7") = TextBox5 Sheet2.Range("P7") = TextBox6 If CheckBox1.Value = True Then Worksheets(2).Range("M7") = "0:30" Else Worksheets(2).Range("M7") = "0:00" End If If CheckBox2.Value = True Then Worksheets(2).Range("R7") = "1000" Else Worksheets(2).Range("R7") = "0" End If If CheckBox3.Value = True Then Worksheets(2).Range("S7") = "3000" Else Worksheets(2).Range("S7") = "0" End If If CheckBox4.Value = True Then Worksheets(2).Range("T7") = "1500" Else Worksheets(2).Range("T7") = "0" End If Unload Me End Sub <ユーザーフォーム2> Private Sub CommandButton1_Click() Sheet2.Range("V7") = TextBox1 Sheet2.Range("W7") = TextBox2 Sheet2.Range("X7") = TextBox3 Unload Me End Sub

  • エクセル マクロ オプションボタン

    エクセル上にユーザーフォーム1を作り OptionButton1~4作成 Captionには OptionButton1)あ OptionButton2)い OptionButton3)う OptionButton4)え と名前を付けてOptionButton1をクリックすると TextBox1に”あ”と表示するようにしたいです! お願いします! 教えて下さい。 {宣言は何?} Dim i As Integer Dim s1 As String For i = 1 To 3 Step 1 s1 = Me.Controls("OptionButton" & i).Caption If Me.Controls("optionbutton" & i) = True Then With UserForm1 .TextBox1.Value = s1 End With End If Next i End Sub 上記の記述では無理でした! 教えて下さい。

  • マクロのフォームから。。。

    マクロのフォームから。。。 マクロのフォームからシートの最終行に入力ができるようにしたいのです。 マクロは下記になります。今の状態ですと、新規入力ボタンを押してデータ登録ボタンを押すと、途中の行に登録がされてしまいます。 暫く考えていましたがどうしてもわからず、また初心者の為できればわかりやすく教えてもらえると幸いです。どうかよろしくお願い致します。 Private Sub CommandButton3_Click() 入力結果 = MsgBox("データを登録しますか", vbYesNo) If 入力結果 = 6 Then If ToggleButton1.Value = True Then 表示行 = Cells(2, 10).Value + 1 Else 表示行 = Cells(1, 10).Value End If If ToggleButton1.Value = True Then データクリア TextBox1.Value = Cells(表示行, 1).Value + 1 Else データ表示 End If 表示行 = Cells(1, 10).Value Cells(表示行, 1).Value = TextBox1.Value Cells(表示行, 2).Value = TextBox2.Value Cells(表示行, 5).Value = TextBox3.Value Cells(表示行, 6).Value = TextBox4.Value Cells(表示行, 7).Value = TextBox5.Value Cells(表示行, 4).Value = ComboBox1.Value If OptionButton1.Value = True Then Cells(表示行, 3).Value = "男" Else Cells(表示行, 3).Value = "女" End If データ表示 End If End Sub ※ 表の範囲:a1:g5(1行目はタイトル) j1:表示行 j2:最終行(counta)

  • CheckBoxがTrueの場合に表示をさせるには?

    VB2008を使用しています。 3つのCheckBox1~3とそれに対応した3つのTextBox1~3を準備し、 Buttonを押すとCheckBoxがTrueのものだけをRichTextBox1に表示 させたくて下記のプログラムを作成しました。 しかしCheckBox1とCheckBox3については上手く作動するのですが CheckBox2について不具合が発生します。 (不具合) CheckBox1=TrueでButtonを押すとRichTextBox1にTextBox1を表示。 その後、CheckBox3=TrueでButtonを押すとRichTextBox1にTextBox1. TextBox3と表示。 ここまでは良いのですが、 CheckBox2=TrueでButtonを押すとRichTextBox1にTextBox2を表示。 その後、CheckBox3=TrueでButtonを押すとRichTextBox1にTextBox2. TextBox2.TextBox3と表示されてしまいTextBox2の内容が重複して しまいまいます。 何か良い解決策がありましたら教えて下さい。 よろしくお願いいたします。 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click     If Me.CheckBox1.Checked = True Then RichTextBox1.Text = TextBox1.Text End If     If Me.CheckBox2.Checked = True Then If RichTextBox1.Text <> "" Then RichTextBox1.Text = RichTextBox1.Text & "."     End If RichTextBox1.Text = RichTextBox1.Text & TextBox2.Text     End If If Me.CheckBox3.Checked = True Then If RichTextBox1.Text <> "" Then RichTextBox1.Text = RichTextBox1.Text & "." End If RichTextBox1.Text = RichTextBox1.Text & TextBox3.Text End If End Sub

  • エクセル ユーザーフォームでVLOOK

    ユーザーフォームのテキストボックスで、ご教示お願いいたします。 現在、以下のようなコードこちらで教えていただきセルに入力をしております。 テキストボックス5に値を入れ、 listのシートから該当する文字をテキストボックス6に表示させたいと思っております。 ■現在のコード '// Private Sub CommandButton1_Click() Dim LastRow As Long 'ここはキャメル形式やパスカル形式にします/大文字は定数です。 Dim i As Long '/テキストボックスに値があるか調べる For i = 1 To 7 Next i '/セルに書き込み With Worksheets("Sheet1") LastRow = .Cells(Rows.Count, 1).End(xlUp).Row + 1 For i = 1 To 7 .Cells(LastRow, i).Value = Me.Controls("TextBox" & i).Value Next End With '/TextBox1-7をクリア If MsgBox("テキストボックスを空にしてよろしいですか?", vbQuestion + vbYesNo) = vbYes Then For i = 1 To 7 Me.Controls("TextBox" & i).Value = "" Next i End If 'TextBox1にフォーカスを移動 Me.TextBox1.SetFocus End Sub ■付け足したいコード Private Sub textbox5_change() Dim temp, x temp = Me.textBox5.Value If IsNumeric(temp) Then temp = Val(temp) x = Application.VLookUp(temp, Sheets("list").Range("a1:b20"), 2, False) If Not IsError(x) Then Me.TextBox6.Value = x Else MsgBox Me.TextBox5.Value & " はリストにありません" End If End Sub 【質問内容】 付け足したいコードは上記の通りですが、どのように付け足せばいいのかが分からず、 困っております。 度々で申し訳ありませんが、テキストボックス5の値を見て、 テキストボックス6に表示させるやり方をご教示お願いいたします。

専門家に質問してみよう