• ベストアンサー

条件判定について

下記のようなコードを作成しましたが、チェックボックスがオンでもオフでもTextBox4.Valueの値が返ります。どこがいけないでしょうか。 Private Sub CommandButton2_Click() If CheckBox1.Visible = True Then Worksheets("sheet1").Range("a1") = TextBox4.Value Else Worksheets("sheet1").Range("a1") = TextBox3.Value End If End Sub

  • m_boy
  • お礼率15% (22/140)

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

  • ベストアンサー
  • papayuka
  • ベストアンサー率45% (1388/3066)
回答No.1

CheckBox1.Visible → CheckBox1.Value ですね。

m_boy
質問者

お礼

有難うございました。解決しました。

その他の回答 (1)

  • imogasi
  • ベストアンサー率27% (4737/17068)
回答No.2

エクセルVBAの話らしいが (1)コマンドボタンのクリク (2)チェックボックスのON・OFF のトリガないし左右でセルA1に値をセットするらしいが、 ーー コントロールツールボックスのテキストボックスのプロパティには LinkedCell にA1を設定すれば、直接値がセットできます。 複雑なことをしなくてもそれで済む場合もあると思うので、紹介しておきます。

関連するQ&A

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

    ユーザーファームを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

  • 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  これをもっとスマートに出来ないでしょうか? 宜しくお願いします。

  • 全く作動しないです。(転記しないです。)

    Private Sub CommandButton1_Click() If CheckBox1 = xlOn Then Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1) .Value = "新規" ElseIf CheckBox1 = xlOff Then Worksheets("sheet1").Range("A" & Rows.Count).End(xlUp).Offset(1) .Value = "リピート" End If End Sub 上記のようなコードですが、まったく作動しないです。 どこが間違っているのでしょうか。

  • マクロ セル指定について

    Sheet(”体温”)のA3に フォームのTextBox1の内容をボタンを押したら 挿入させたいです。! 下の記述では上手くいきません 教えて下さい! Private Sub CommandButton1_Click() With Worksheets("体温") .Range(A3).Value = TextBox1.Value End With End Sub お願いします!

  • サブルーチンの使い方

    こんにちは 以前 このサイトでサブルーチンを教えて頂いたのですが 今回は、このサブルーチンを使って Commandbuttonに Sheets("20年7月度").Cells(2, 6).Value = Cells(2, 6) & Mojiを 入れたいのですが、どのようにすれば いいですか? 宜しくお願い致します。 Private Sub mySub(Moji) '数字 If Me.MultiPage1.Value = 0 Then TextBox3.Value = TextBox3 & Moji Else: TextBox1.Value = TextBox1 & Moji End If End Sub Private Sub CommandButton1_Click() 'サブルーチン Call mySub("1") End Sub Private Sub CommandButton2_Click() Call mySub("2") End Sub Private Sub CommandButton3_Click() Call mySub("3") End Sub Private Sub CommandButton4_Click() Call mySub("4") End Sub Private Sub CommandButton5_Click() Call mySub("5") End Sub Private Sub CommandButton6_Click() Call mySub("6") End Sub Private Sub CommandButton7_Click() Call mySub("7") End Sub Private Sub CommandButton8_Click() Call mySub("8") End Sub Private Sub CommandButton9_Click() Call mySub("9") End Sub Private Sub CommandButton10_Click() Call mySub("0") End Sub Private Sub CommandButton11_Click() Call mySub("00") End Sub Private Sub CommandButton12_Click() Call mySub("000") End Sub

  • エクセル チェックボックスの解除について(VBA)

    YES/NOを入力させる為の下記のVBAにおいて、チェックボックス1をチェックすると、アの部分でチェックボックス2の解除を行う関係で?、シート上でチェックボックス2を操作していないのにもかかわらず、勝手にCheckBox2_Click()に入り、命令文イを実行してしまいます。 ただ単にSub CheckBox1_Click()のルーチンの最後までの処理で終わりたいのですが、どうしたらよいのでしょうか。 Private Sub CheckBox1_Click() If CheckBox1 = True Then Sheets("sheet1").Range("A1") = 1 Sheets("sheet1").Range("A2") = 0 CheckBox2 = False・・・ア Else Sheets("sheet1").Range("A1") = "" End If End Sub Private Sub CheckBox2_Click() If CheckBox2 = True Then Sheets("sheet1").Range("A1") = 0 Sheets("sheet1").Range("A2") = 1 CheckBox1 = False Else Sheets("sheet1").Range("A2") = ""・・・イ End If End Sub

  • VBA controlsouce の使い方

    エクセル2002使用です。 個人情報リストのようなものを作ってます。 ユーザーフォームにTextBox1とCommandButton1をつくり、そこにセルに既に入力されているデーターを入力したいのですが、うまくいきません。 Private Sub CommandButton1_Click() UserForm1.TextBox1.ControlSource = _ Worksheets(1).Range("B5").Value End Sub どこか悪いのか全くわからず困ってます。 よろしくお願いします。

  • エクセル VBAのチェックボックスについて

    お読みくださり、ありがとうございます。 エクセル初心者でございます。 エクセルのマクロなのですが、 お詳しい方、是非教えて欲しいです!汗 調子に乗って入力フォームなるものを作りました。 入力フォームの中にて、チェックボックスで「ある」「なし」の項目を入れてみたのですが、チェックしていないのに、値が入る現象が起きています汗 以下、素人が書いたコードを恥を承知で記載させていただきます。 Private Sub CheckBox1_Click() If CheckBox1.Value = True Then OK = "○" End If End Sub Private Sub CheckBox2_Click() If CheckBox2.Value = True Then NO = "×" End If End Sub Private Sub UserForm_Click() End Sub '以下のコードは、登録ボタンがクリックされたときの処理! Private Sub 登録ボタン_Click() If TextBox1.Text = "" Then MsgBox "グッズ名を入力してください。" Exit Sub End If If TextBox2.Text = "" Then MsgBox "アプローチ先を入力してください。" Exit Sub End If With Worksheets("協賛グッズ") With Cells(Rows.Count, 2).End(xlUp) .Offset(1, 0).Value = TextBox1.Text .Offset(1, 1).Value = mori .Offset(1, 2).Value = mori2 .Offset(1, 3).Value = TextBox2.Text .Offset(1, 5).Value = TextBox3.Text .Offset(1, 6).Value = TextBox4.Text .Offset(1, 7).Value = TextBox5.Text .Offset(1, 8).Value = TextBox6.Text End With End With TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" TextBox5.Text = "" TextBox6.Text = "" CheckBox1.Value = False CheckBox2.Value = False End Sub 以上です。 おかしなところ満載かと思いますが、 チェックを入れた項目だけ値を入れたいと考えております。 おわかりになるかたおりましたら何卒お助けください汗 よろしくお願いいたします。

  • EXCEL2003 VBA リストボックス

    お世話になります。 質問の内容についてですが、 現在VBAの勉強をしながらデータベースを組んでいます。 そこで、次のようなものを作っています。 Sheet1に於いて、     A    B     C 1  追番  名称   個数 2   1   りんご   1個 3   2   みかん  2個 4   3   なし    3個 5   4   なす    5個 というような表があるとします。 そして、VBAでフォームをつくり、TextBox1(名称入力用)、TextBox2(個数入力用)、ListBox1(すでに入力されているもの及び追加分のリスト用)、CommandButton1(入力された「名称」と「個数」をSheet1の表の一番下に追加)、CommandButton2(フォームを閉じる)という構成にしています。 また、Sheet1上にコマンドボタンを作っており、そのボタンを押すとフォームを呼出すようにしています。 流れとしては、既存の表に追加する場合、コマンドボタンを押してフォームを呼出し、テキストボックスに入力した内容をSheet1の一番下に追加する。また、フォーム上のリストボックスでも現在の表の内容を見る事ができる、というものです。 フォームでのコードは以下のようにしています。 Private Sub CommandButton1_Click() If TextBox1.Value = "" Then MsgBox "「名称」は必須項目です。" End If If TextBox2.Value = "" Then MsgBox "「個数」は必須項目です。" End If If TextBox2.Value = "0" Then MsgBox "「個数」に0は登録できません。" End If Lrow = Range("B2").CurrentRegion.Rows.Count Range("B" & Lrow + 1).Value = TextBox1.Value Range("C" & Lrow + 1).Value = TextBox2.Value End Sub Private Sub CommandButton2_Click() Unload UserForm1 End Sub Private Sub UserForm_Initialize() Dim b As Long Dim a() As String ReDim a(1 To 100) UserForm1.ListBox1.ColumnCount = 2 UserForm1.ListBox1.List = Worksheets(Sheet1).Range("B2:C").Value For i = 2 To 104 If Range("B" & i) = "" Then ListBox1.AddItem Range("B" & i).Value ListBox1.AddItem Range("c" & i).Value b = b + 1 a(b) = Range("C" & i).Value End If Next i End Sub このコードでSheet1上のコマンドボタンを押して実行しようとするとエラーが出てしまいます。 エラーの原因は何なのでしょうか? (なお、コマンドボタンのコードは「UserForm1.Show」のみです。 コード自体は本などを読みながら似たようなVBAを使った物を参考にしています。

  • マクロにおける条件文の作成の件

    以下の様に条件付きの計算式を作成しました。CommandButton3を押しても 計算しなかったり、TextBox3.Value > TextBox1 ではないときでもエラー メッセージが出ます。どこに欠点があるのか教えて下さい。 Private Sub CommandButton3_Click() Dim row As Integer If TextBox1.Value = Empty Then MsgBox ("Aが空欄です") Exit Sub End If If TextBox2.Value = Empty Then MsgBox ("Bが空欄です") Exit Sub End If If TextBox3.Value = Empty Then MsgBox ("Cが空欄です") Exit Sub End If If TextBox4.Value = Empty Then MsgBox ("Dが空欄です") Exit Sub End If If TextBox3.Value > TextBox1.Value Then MsgBox ("Cの値をAの値より小さくしましょう!") Exit Sub End If If TextBox4.Value > TextBox2.Value Then MsgBox ("Dの値をBの値より小さくしましょう!") Exit Sub End If TextBox5 = Round(TextBox1 * TextBox2 - (TextBox1 - TextBox3) * (TextBox2 - TextBox4) / 2, 0) End Sub

専門家に質問してみよう