質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

マクロ

定義された処理手続きに応じて、どのような一連の処理を行うのかを特定させるルールをマクロと呼びます。

ラジオボタン

ラジオボタンはフォームに使われる要素のひとつであり、ユーザに限られた選択肢からひとつの答えを選んでもらうというものです。

Q&A

解決済

2回答

1517閲覧

オプションボタンの書き方

chie3

総合スコア8

VBA

VBAはオブジェクト指向プログラミング言語のひとつで、マクロを作成によりExcelなどのOffice業務を自動化することができます。

マクロ

定義された処理手続きに応じて、どのような一連の処理を行うのかを特定させるルールをマクロと呼びます。

ラジオボタン

ラジオボタンはフォームに使われる要素のひとつであり、ユーザに限られた選択肢からひとつの答えを選んでもらうというものです。

0グッド

0クリップ

投稿2020/05/21 02:41

userform のオプションボタンの記入の仕方

※印の所をオプションボタンで作りたいのですが…
1口座振込 2現金 3その他
という風に作りたいのですがどうやればいいのでしょうか?

Option Explicit
'===================================================================================================
Public g_swOK As Byte ' フォームで登録が押されたかを判定する

Public Sub TOUROKU(lngRow As Long)
'-----------------------------------------------------------------------------------------------
With FRM_USER
' 新規登録か判断
If ((lngRow = 1) Or (Cells(lngRow, 1).Value = "")) Then
' 見出しか未登録行の場合は新規登録と判断
lngRow = 0
.ComboBox5.Text = "" '下請業者
.TXT_CODE.Text = "" ' 注文番号
.ComboBox2 = "" '注文 年
.ComboBox3 = "" '注文 月
.ComboBox4 = "" '注文 日
.ComboBox1 = "" '担当者
.TXT_hin1 = "" '品名1
.TXT_su1 = "" '数量1
.TextBox1 = "" '単価1
.TXT_hin2 = "" '品名2
.TXT_su2 = "" '数量2
.TextBox2 = "" '単価2
.TXT_hin3 = "" '品名3
.TXT_su3 = "" '数量3
.TextBox3 = "" '単価3
.TXT_hin4 = "" '品名4
.TXT_su4 = "" '数量4
.TextBox4 = "" '単価4
.TextBox6 = "" '品名5
.TXT_su5 = "" '数量5
.TextBox5 = "" '単価5
.ComboBox6 = "" '納期 年
.ComboBox7 = "" '納期 月
.ComboBox8 = "" '納期 日
.Text_ba = "" '納期場所
※ .ComboBox9 = "" '支払方法
.Text_si = "" '支払その他
.TextB1 = "" '備考1
.TextB2 = "" '備考2
.TextB3 = "" '備考3
.TextB4 = "" '備考4
.TextB5 = "" '備考5
.ComboBox10 = "" '
.ComboBox11 = "" '
.ComboBox12 = "" '

Else ' 既存行の場合は修正と判断 .ComboBox5.Text = Cells(lngRow, 1).Value ' 下請業者 .TXT_CODE.Text = Cells(lngRow, 2).Value '注文番号 .ComboBox2.Text = Cells(lngRow, 3).Value ' 注文 年 .ComboBox3.Text = Cells(lngRow, 4).Value '注文 月 .ComboBox4.Text = Cells(lngRow, 5).Value '注文 日 .ComboBox1.Text = Cells(lngRow, 6).Value '担当者 .TXT_hin1.Text = Cells(lngRow, 7).Value '品名1 .TXT_su1.Text = Cells(lngRow, 8).Value '数量1 .TextBox1.Text = Cells(lngRow, 9).Value '単価1 .TXT_hin2.Text = Cells(lngRow, 10).Value '品名2 .TXT_su2.Text = Cells(lngRow, 11).Value '数量2 .TextBox2.Text = Cells(lngRow, 12).Value '単価2 .TXT_hin3.Text = Cells(lngRow, 13).Value '品名3 .TXT_su3.Text = Cells(lngRow, 14).Value '数量3 .TextBox3.Text = Cells(lngRow, 15).Value '単価3 .TXT_hin4.Text = Cells(lngRow, 16).Value '品名4 .TXT_su4.Text = Cells(lngRow, 17).Value '数量4 .TextBox4.Text = Cells(lngRow, 18).Value '単価4 .TextBox6 = Cells(lngRow, 19).Value '品名5 .TXT_su5.Text = Cells(lngRow, 20).Value '数量5 .TextBox5.Text = Cells(lngRow, 21).Value '単価5 .ComboBox6.Text = Cells(lngRow, 22).Value '納期 年 .ComboBox7.Text = Cells(lngRow, 23).Value '納期 月 .ComboBox8.Text = Cells(lngRow, 24).Value '納期 日 .Text_ba = Cells(lngRow, 25).Value '納期場所 ※ .ComboBox9 = Cells(lngRow, 26).Value '支払方法 .Text_si = Cells(lngRow, 27).Value '支払3 .TextB1 = Cells(lngRow, 28).Value '備考1 .TextB2 = Cells(lngRow, 29).Value '備考2 .TextB3 = Cells(lngRow, 30).Value '備考3 .TextB4 = Cells(lngRow, 31).Value '備考4 .TextB5 = Cells(lngRow, 32).Value '備考5 .ComboBox10 = Cells(lngRow, 33).Value ' .ComboBox11 = Cells(lngRow, 34).Value ' .ComboBox12 = Cells(lngRow, 35).Value ' End If ' フォームを表示 g_swOK = 0 .Show ' 登録ボタンが押されていない場合は以降の処理はしない If g_swOK <> 1 Then Exit Sub ' 新規登録の場合は未登録行を探す If lngRow = 0 Then lngRow = 2 ' 下請業者列未登録を判定 Do While Cells(lngRow, 1).Value <> "" lngRow = lngRow + 1 Loop End If ' シート上に登録 ActiveSheet.Unprotect Cells(lngRow, 1).Value = Trim(.ComboBox5.Text) ' 下請業者 Cells(lngRow, 2).Value = Trim(.TXT_CODE.Text) ' 注文番号 Cells(lngRow, 3).Value = Trim(.ComboBox2.Text) ' 注文 年 Cells(lngRow, 4).Value = Trim(.ComboBox3.Text) '注文 月 Cells(lngRow, 5).Value = Trim(.ComboBox4.Text) '注文 日 Cells(lngRow, 6).Value = Trim(.ComboBox1.Text) '担当者 Cells(lngRow, 7).Value = Trim(.TXT_hin1.Text) '品名1 Cells(lngRow, 8).Value = Trim(.TXT_su1.Text) '数量1 Cells(lngRow, 9).Value = Trim(.TextBox1.Text) '単価1 Cells(lngRow, 10).Value = Trim(.TXT_hin2.Text) '品名2 Cells(lngRow, 11).Value = Trim(.TXT_su2.Text) '数量2 Cells(lngRow, 12).Value = Trim(.TextBox2.Text) '単価2 Cells(lngRow, 13).Value = Trim(.TXT_hin3.Text) '品名3 Cells(lngRow, 14).Value = Trim(.TXT_su3.Text) '数量3 Cells(lngRow, 15).Value = Trim(.TextBox3.Text) '単価3 Cells(lngRow, 16).Value = Trim(.TXT_hin4.Text) '品名4 Cells(lngRow, 17).Value = Trim(.TXT_su4.Text) '数量4 Cells(lngRow, 18).Value = Trim(.TextBox4.Text) '単価4 Cells(lngRow, 19).Value = Trim(.TextBox6.Text) '品名5 Cells(lngRow, 20).Value = Trim(.TXT_su5.Text) '数量5 Cells(lngRow, 21).Value = Trim(.TextBox5.Text) '単価5 Cells(lngRow, 22).Value = Trim(.ComboBox6.Text) '納期 年 Cells(lngRow, 23).Value = Trim(.ComboBox7.Text) '納期 月 Cells(lngRow, 24).Value = Trim(.ComboBox8.Text) '納期 日 Cells(lngRow, 25).Value = Trim(.Text_ba.Text) '納期場所 ※ Cells(lngRow, 26).Value = Trim(.ComboBox9.Text) '支払方法 Cells(lngRow, 27).Value = Trim(.Text_si.Text) '支払方法3 Cells(lngRow, 28).Value = Trim(.TextB1.Text) '備考1 Cells(lngRow, 29).Value = Trim(.TextB2.Text) '備考2 Cells(lngRow, 30).Value = Trim(.TextB3.Text) '備考3 Cells(lngRow, 31).Value = Trim(.TextB4.Text) '備考4 Cells(lngRow, 32).Value = Trim(.TextB5.Text) '備考5 Cells(lngRow, 33).Value = Trim(.ComboBox10.Text) ' Cells(lngRow, 34).Value = Trim(.ComboBox11.Text) ' Cells(lngRow, 35).Value = Trim(.ComboBox12.Text) ' ActiveSheet.Protect End With

End Sub

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

自己解決

ネットで調べて、試してみたところ解決しました。

投稿2020/05/22 00:22

chie3

総合スコア8

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

コントロールIDを書かないと。。。

以下はサンプルです。

VBA

1.OptionButton1.Caption = "1口座振込" 2.OptionButton2.Caption = "2現金" 3.OptionButton3.Caption = "3その他"

投稿2020/05/21 02:56

ttyp03

総合スコア16998

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

chie3

2020/05/21 04:24

Option Explicit '=================================================================================================== Public g_swOK As Byte ' フォームで登録が押されたかを判定する Public Sub TOUROKU(lngRow As Long) '----------------------------------------------------------------------------------------------- With FRM_USER ' 新規登録か判断 If ((lngRow = 1) Or (Cells(lngRow, 1).Value = "")) Then ' 見出しか未登録行の場合は新規登録と判断 lngRow = 0 .ComboBox5.Text = "" '下請業者 .TXT_CODE.Text = "" ' 注文番号 .ComboBox2 = "" '注文 年 .ComboBox3 = "" '注文 月 .ComboBox4 = "" '注文 日 .ComboBox1 = "" '担当者 .TXT_hin1 = "" '品名1 .TXT_su1 = "" '数量1 .TextBox1 = "" '単価1 .TXT_hin2 = "" '品名2 .TXT_su2 = "" '数量2 .TextBox2 = "" '単価2 .TXT_hin3 = "" '品名3 .TXT_su3 = "" '数量3 .TextBox3 = "" '単価3 .TXT_hin4 = "" '品名4 .TXT_su4 = "" '数量4 .TextBox4 = "" '単価4 .TextBox6 = "" '品名5 .TXT_su5 = "" '数量5 .TextBox5 = "" '単価5 .ComboBox6 = "" '納期 年 .ComboBox7 = "" '納期 月 .ComboBox8 = "" '納期 日 .Text_ba = "" '納期場所 ※ .OptionButton1 = "" '口座振込 .OptionButton2 = "" ’現金 .OptionButton3 = "" ’その他 .Text_si = "" '支払その他 .TextB1 = "" '備考1 .TextB2 = "" '備考2 .TextB3 = "" '備考3 .TextB4 = "" '備考4 .TextB5 = "" '備考5 .ComboBox10 = "" ' .ComboBox11 = "" ' .ComboBox12 = "" ' Else ' 既存行の場合は修正と判断 .ComboBox5.Text = Cells(lngRow, 1).Value ' 下請業者 .TXT_CODE.Text = Cells(lngRow, 2).Value '注文番号 .ComboBox2.Text = Cells(lngRow, 3).Value ' 注文 年 .ComboBox3.Text = Cells(lngRow, 4).Value '注文 月 .ComboBox4.Text = Cells(lngRow, 5).Value '注文 日 .ComboBox1.Text = Cells(lngRow, 6).Value '担当者 .TXT_hin1.Text = Cells(lngRow, 7).Value '品名1 .TXT_su1.Text = Cells(lngRow, 8).Value '数量1 .TextBox1.Text = Cells(lngRow, 9).Value '単価1 .TXT_hin2.Text = Cells(lngRow, 10).Value '品名2 .TXT_su2.Text = Cells(lngRow, 11).Value '数量2 .TextBox2.Text = Cells(lngRow, 12).Value '単価2 .TXT_hin3.Text = Cells(lngRow, 13).Value '品名3 .TXT_su3.Text = Cells(lngRow, 14).Value '数量3 .TextBox3.Text = Cells(lngRow, 15).Value '単価3 .TXT_hin4.Text = Cells(lngRow, 16).Value '品名4 .TXT_su4.Text = Cells(lngRow, 17).Value '数量4 .TextBox4.Text = Cells(lngRow, 18).Value '単価4 .TextBox6 = Cells(lngRow, 19).Value '品名5 .TXT_su5.Text = Cells(lngRow, 20).Value '数量5 .TextBox5.Text = Cells(lngRow, 21).Value '単価5 .ComboBox6.Text = Cells(lngRow, 22).Value '納期 年 .ComboBox7.Text = Cells(lngRow, 23).Value '納期 月 .ComboBox8.Text = Cells(lngRow, 24).Value '納期 日 .Text_ba = Cells(lngRow, 25).Value '納期場所 ※ .OptionButton1 = Cells(ingRow, 26).value '口座振込   .OptionButton2 = Cells(ingRow, 26).value '現金 .OptionButton3 = Cells(ingRow, 26).value    'その他 .Text_si = Cells(lngRow, 27).Value '支払3 .TextB1 = Cells(lngRow, 28).Value '備考1 .TextB2 = Cells(lngRow, 29).Value '備考2 .TextB3 = Cells(lngRow, 30).Value '備考3 .TextB4 = Cells(lngRow, 31).Value '備考4 .TextB5 = Cells(lngRow, 32).Value '備考5 .ComboBox10 = Cells(lngRow, 33).Value ' .ComboBox11 = Cells(lngRow, 34).Value ' .ComboBox12 = Cells(lngRow, 35).Value ' End If ' フォームを表示 g_swOK = 0 .Show ' 登録ボタンが押されていない場合は以降の処理はしない If g_swOK <> 1 Then Exit Sub ' 新規登録の場合は未登録行を探す If lngRow = 0 Then lngRow = 2 ' 下請業者列未登録を判定 Do While Cells(lngRow, 1).Value <> "" lngRow = lngRow + 1 Loop End If ' シート上に登録 ActiveSheet.Unprotect Cells(lngRow, 1).Value = Trim(.ComboBox5.Text) ' 下請業者 Cells(lngRow, 2).Value = Trim(.TXT_CODE.Text) ' 注文番号 Cells(lngRow, 3).Value = Trim(.ComboBox2.Text) ' 注文 年 Cells(lngRow, 4).Value = Trim(.ComboBox3.Text) '注文 月 Cells(lngRow, 5).Value = Trim(.ComboBox4.Text) '注文 日 Cells(lngRow, 6).Value = Trim(.ComboBox1.Text) '担当者 Cells(lngRow, 7).Value = Trim(.TXT_hin1.Text) '品名1 Cells(lngRow, 8).Value = Trim(.TXT_su1.Text) '数量1 Cells(lngRow, 9).Value = Trim(.TextBox1.Text) '単価1 Cells(lngRow, 10).Value = Trim(.TXT_hin2.Text) '品名2 Cells(lngRow, 11).Value = Trim(.TXT_su2.Text) '数量2 Cells(lngRow, 12).Value = Trim(.TextBox2.Text) '単価2 Cells(lngRow, 13).Value = Trim(.TXT_hin3.Text) '品名3 Cells(lngRow, 14).Value = Trim(.TXT_su3.Text) '数量3 Cells(lngRow, 15).Value = Trim(.TextBox3.Text) '単価3 Cells(lngRow, 16).Value = Trim(.TXT_hin4.Text) '品名4 Cells(lngRow, 17).Value = Trim(.TXT_su4.Text) '数量4 Cells(lngRow, 18).Value = Trim(.TextBox4.Text) '単価4 Cells(lngRow, 19).Value = Trim(.TextBox6.Text) '品名5 Cells(lngRow, 20).Value = Trim(.TXT_su5.Text) '数量5 Cells(lngRow, 21).Value = Trim(.TextBox5.Text) '単価5 Cells(lngRow, 22).Value = Trim(.ComboBox6.Text) '納期 年 Cells(lngRow, 23).Value = Trim(.ComboBox7.Text) '納期 月 Cells(lngRow, 24).Value = Trim(.ComboBox8.Text) '納期 日 Cells(lngRow, 25).Value = Trim(.Text_ba.Text) '納期場所 Cells(lngRow, 26).Value = Trim(.OptionButton1.Text) '口座振込 Cells(lngRow, 26).Value = Trim(.OptionButton2.Text)   '現金 Cells(lngRow, 26).Value = Trim(.OptionButton3.Text)   'その他 Cells(lngRow, 27).Value = Trim(.Text_si.Text) '支払方法3 Cells(lngRow, 28).Value = Trim(.TextB1.Text) '備考1 Cells(lngRow, 29).Value = Trim(.TextB2.Text) '備考2 Cells(lngRow, 30).Value = Trim(.TextB3.Text) '備考3 Cells(lngRow, 31).Value = Trim(.TextB4.Text) '備考4 Cells(lngRow, 32).Value = Trim(.TextB5.Text) '備考5 Cells(lngRow, 33).Value = Trim(.ComboBox10.Text) ' Cells(lngRow, 34).Value = Trim(.ComboBox11.Text) ' Cells(lngRow, 35).Value = Trim(.ComboBox12.Text) ' ActiveSheet.Protect End With End Sub こういうことでしょうか? これだとエラーになります。
ttyp03

2020/05/21 04:51

エラーの内容を書いてくれないと。 OptionButton1~3は作ってあるんですよね? あと、回答したのはこれですけど。 .OptionButton1.Caption = "1口座振込" .OptionButton2.Caption = "2現金" .OptionButton3.Caption = "3その他"
chie3

2020/05/21 06:45

オプションボタンは作ってあります。 .OptionButton1.Caption = "1口座振込" .OptionButton2.Caption = "2現金" .OptionButton3.Caption = "3その他" はどこに入力すればいいのでしょうか? Cells(lngRow, 26).Value = Trim(.OptionButton1.Text)を Cells(lngRow, 26).value = Trim(.OptionButton1Caption = "1口座振込”) とすればいいのでしょうか? chieko3
ttyp03

2020/05/21 07:35

> どこに入力すればいいのでしょうか? いや、それを私に聞かれても。。。 どういう仕様でどういう条件の時に行いたいのですか?
chie3

2020/05/21 23:54

細かい事が、わからないので質問したのに、どういう仕様と言われても難しいです。 作り方を聞きたかったので、これですって、書かれましても、どう書き込めばいいかわかりません。 今の所、全部に教えてもらったものを書くとエラーが出ますが、見よう見まねで作りましたので、オプションボタンの書き方を細かく教えて下さるとありがたかったのですが、私には今の所難しすぎて理解できません。
ttyp03

2020/05/22 00:26

ああ、質問の解釈が間違っていたのでしょうか。 オプションボタンは既にあるので、それをコンボボックスからオプションボタンに変更したい。 ということなのでしょうか? 色々情報が不足しているので確認させてください。 オプションボタンは既に存在していた? オプションボタンのコントロールIDは? オプションボタンのキャプションは設定済み? フォームの入力状態をシートに保持しているようですが、オプションボタンは何を登録する想定ですか?番号?名前? オプションの初期状態はどれがデフォルト?それとも これくらい情報が不足しています。 質問を読んだ限りでは「オプションにキャプションを設定したい」くらいしか読み取れませんでしたので、最初の回答になりました。 色々情報が不足していますが、なんとなく聞きたいこともわかってきたので、仮に以下の仕様とした場合のサンプルコードを書いておきます。 ・キャプションは設定済み ・オプションボタンの初期値は「1口座振込」 ・シートに保存するのはキャプション先頭の番号 サンプルコード With FRM_USER ' 新規登録か判断 If ((lngRow = 1) Or (Cells(lngRow, 1).Value = "")) Then ' 見出しか未登録行の場合は新規登録と判断 .OptionButton1.Value = True '支払方法 Else ' 既存行の場合は修正と判断 Select Case Cells(lngRow, 26).Value Case 1 .OptionButton1.Value = True Case 2 .OptionButton2.Value = True Case 3 .OptionButton3.Value = True End Select End If ' 登録ボタンが押されていない場合は以降の処理はしない If g_swOK <> 1 Then Exit Sub ' シート上に登録 If .OptionButton1.Value = True Then Cells(lngRow, 26).Value = 1 Else If .OptionButton2.Value = True Then Cells(lngRow, 26).Value = 2 Else If .OptionButton1.Value = True Then Cells(lngRow, 26).Value = 3 End If End If End With
ttyp03

2020/05/22 00:27

て書いてる間に自己解決とはね。
chie3

2020/05/22 01:36

ttyp03様 ありがとうございます。 参考になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問