[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『VBA条件式?』(初心者)
ユーザーフォームにTextBox1、TextBox2、TextBox3、TextBox4の
4つテキストボックスがあるとします。
さらにTextBox1には1000(固定)という値が
TextBox2には2000(固定)という値が
TextBox3には3000という値が
TextBox4には1000(固定)という値が
入っています。TextBox3に関しては固定ではなく、自由に変更可能な値を入れられます。
TextBox4にも値が入っているのですが、TextBox1、TextBox2、TextBox3に値がある時のみ
TextBox4の値を0に変更したいのです。
どういうコードを書けば良いでしょうか?
結局 TextBox3 次第であれば、こういうことでしょうか。 (Mook)
Private Sub TextBox3_AfterUpdate()
If TextBox3.Value = "" Then
TextBox4.Value = "1000"
Else
TextBox4.Value = "0"
End If
End Sub
試してないけど・・・ Mookさんの拝借
Private Sub TextBox3_AfterUpdate()
Dim x As Long
Select Case ""
Case TextBox3.Value: x = 0
Case TextBox2.Value: x = 0
Case TextBox1.Value: x = 0
Case Else: x = 1000
End Select
TextBox1.Value = x
End Sub
(稲葉)
ん?ということは説明中の「固定」とはどういう意味でしょう。 というのはさておき、 1〜3があるときという条件であれば、こんな感じでしょうか。 #ぶつかったけれど、そのままアップします。 (Mook)
Private Sub TextBox1_AfterUpdate()
CheckAndSetTextBox4
End Sub
Private Sub TextBox2_AfterUpdate()
CheckAndSetTextBox4
End Sub
Private Sub TextBox3_AfterUpdate()
CheckAndSetTextBox4
End Sub
Private Sub CheckAndSetTextBox4()
If TextBox1.Value <> "" And TextBox2.Value <> "" And TextBox3.Value <> "" Then
TextBox4.Value = "0"
Else
TextBox4.Value = "1000"
End If
End Sub
TextBox1に値があり、かつTextBox2にも値があり、かつTextBox3にも値があるときのみTextBox4とTextBox5の値を0にしたいです。
コード初心者で追加がいまいちわかりません。教えてくださいお願いしますm(_ _)m
(初心者)
ランダムって、手入力ってことだよね?
Private Sub TextBox3_AfterUpdate() 'テキストボックス3が更新された後、実行します。
Dim x As Long
Select Case "" '各caseの値が、""の時、True(:の後の処理を実行)を返します
Case TextBox3.Value: Exit Sub 'テキストボックス3が""処理を中断します。
Case TextBox2.Value: Exit Sub 'テキストボックス2が""処理を中断します。
Case TextBox1.Value: Exit Sub 'テキストボックス1が""処理を中断します。
Case Else: x = 0 '↑のいずれにも当てはまらない場合、xに0を代入します。
End Select
'テキストボックス4と5にxを代入します。
TextBox4.Value = x
TextBox5.Value = x
End Sub
というか、前回提示したの、間違ってたね・・・ごめん (稲葉)
書き方としてはMookさんのほうが正しいです。 例えばテキストボックス4と5もチェックボックスで値が入るようにしているなら、 テキストボックス3が入力された後に、チェックを入れられると0になりません。
なので、Mookさんのように、別にモジュールを作って、変更があった場合、都度そのモジュールを 実行させないと思わぬミスがでます。 (稲葉)
txt1=テキストボックス txt2=テキストボックス txt3=テキストボックス
txt4=テキストボックス txt5=テキストボックス
txtkei=合計の値が表示するテキストボックス
chk1=チェックボックス(txt1付随) chk2=チェックボックス(txt2付随)
chk3=チェックボックス(txt3付随)
chk4=チェックボックス(txt4付随)
cmb1=合計を計算するボタン
Private Sub txt3_AfterUpdate()
CheckAndSettxt1
CheckAndSettxt2
End Sub
Private Sub txt4_Change()
CheckAndSettxt1
CheckAndSettxt2
End Sub
Private Sub txt5_Change()
CheckAndSettxt1
CheckAndSettxt2
End Sub
Private Sub CheckAndSettxt1()
If txt3.Value <> "" And txt4.Value <> "" And txt5.Value <> "" Then
txt1.Value = "0"
Else
txt1.Value = "100"
End If
End Sub
Private Sub CheckAndSettxt2()
If txt3.Value <> "" And txt4.Value <> "" And txt5.Value <> "" Then
txt2.Value = "0"
Else
txt2.Value = "200"
End If
End Sub
Private Sub chk4_Click()
If chk4.Value = True Then
txt4 = 400
Else
txt4 = ""
End If
End Sub
Private Sub chk3_Click()
If chk3.Value = True Then
txt3 = 300
Else
txt3 = ""
End If
End Sub
Private Sub chk1_Click()
If chk1.Value = True Then
txt1 = 100
Else
txt1 = ""
End If
End Sub
Private Sub chk2_Click()
If chk2.Value = True Then
txt2 = 200
Else
txt2 = ""
End If
End Sub
Private Sub cmb1_Click()
With UserForm1
.txtkei.Value = Val(.txt1.Value) + Val(.txt2.Value) + Val(.txt3.Value) _
+ Val(.txt4.Value) + Val(.txt5.Value)
End With
End Sub
ここまででtxt1とtxt2はtxt1,2に値がありかつtxt3,4,5に値がある時のみ0に
出来るんですが、
txt1,2に値がありかつtxt3,4に値がある時はtxt1だけを0にしたいのですが
その場合どうしたら良いでしょう?
ご希望通りかわかりませんが。。。
'#####ここからテキストボックスが変わった時の処理
Private Sub txt3_AfterUpdate()
Call CheckAndSettxt1
End Sub
Private Sub txt4_Change()
Call CheckAndSettxt1
End Sub
Private Sub txt5_Change()
CheckAndSettxt1
End Sub
Private Sub CheckAndSettxt1()
If txt5.Value <> "" Then 'txt5が空白じゃなければ
If txt3.Value <> "" And txt4.Value <> "" Then 'txt3,4が空白じゃなければ
txt1.Value = "0": chk1.Value = False
txt2.Value = "0": chk2.Value = False
End If
ElseIf txt2.Value <> "" & txt3.Value <> "" And txt4.Value <> "" Then 'txt5が空白且つ、2,3,4が空白じゃなければ
txt1.Value = "0": chk1.Value = False
Else '上記のいずれかでもなければ
txt1.Value = "100": chk1.Value = True
txt2.Value = "200": chk2.Value = True
End If
End Sub
'#####ここまでテキストボックスが変わった時の処理
'#####ここからチェックボックス制御#####
Private Sub chk1_Click()
Call cl_ch(1)
End Sub
Private Sub chk2_Click()
Call cl_ch(2)
End Sub
Private Sub chk3_Click()
Call cl_ch(3)
End Sub
Private Sub chk4_Click()
Call cl_ch(4)
End Sub
Private Sub cl_ch(i As Variant)
Dim c, t
c = Array(, chk1, chk2, chk3, chk4)
t = Array(, txt1, txt2, txt3, txt4)
v = Array(, 100, 200, 300, 400)
t(i).Value = IIf(c(i).Value, v(i), "")
End Sub
'#####ここまでチェックボックス制御#####
Private Sub cmb1_Click()
Dim t, c
Dim s As Long
With UserForm1
t = Array(.txt1, .txt2, .txt3, .txt4)
For Each c In t
s = s + Val(c.Value)
Next
.txtkei.Value = s
End With
End Sub
(稲葉)再差し替え 15:44
Call cl_sh(1)
End Sub
このコードのshと(1)が意味がわからず・・エラー出て、対処出来ないです。
チェックボックスをクリックしたら
コンパイルエラー SubまたはFunctionが定義されていません
と出ます。
どうしましょう・・
(初心者)
ごめんなさい、書き損じです。 Call cl_sh(1) は Call cl_ch(1) の間違いです。 ↑のコード書き直したので、一度入れ直してください。
似たような処理をするときは、別のモジュールを設けてそこで処理させることが一般的?です。
↑の場合は、(1)の1を引数として、Sub cl_ch の処理に投げています。
で、何に使うかと言うと、Sub cl_chの中で、テキストボックスや入力したい値を配列に入れ、
同じ処理をまとめようとしています。
c = Array(, chk1, chk2, chk3, chk4)
t = Array(, txt1, txt2, txt3, txt4)
v = Array(, 100, 200, 300, 400)
↑でカンマ(,)から始まっている理由は、Arrayで配列を作ると、0から始まる配列になるので、
0は使わないからカンマで飛ばしています。
ごめんなさいね。 同じ条件準備するのかったるくて理論だけでたてちゃったので・・・ (稲葉)
s = s + Val(v.Value) ここは s = s + Val(c.Value) の間違いです。度々すみません。
>chに訂正したらチェックは入るんですが、値が出ません・・ ここはちょっと解せない・・・ 一度環境作って試します。
(稲葉)
>chに訂正したらチェックは入るんですが、値が出ません・・ これは試したけど出ましたよ? テキストボックスの名前とか合ってますか?
それと、実際にいじってみてかなり変な動きしていますが、本当にこれでいいの・・・? (稲葉)
ほんとだ!すみませんねぇそそっかしくて
でもやっぱりおかしい。 5を空白にしておいて、1234とクリックすると、4をクリックしたときに1が消える でも1をクリックすると、1にはイベントを入れてないので1にも値が入る
5が何かしら入力されていた場合も同様で、1234の4で12が消えて、12にチェック入れると 12にも値が入る
これでいいの?
(稲葉)
txt1.Value = "100": chk1.Value = True
txt2.Value = "200": chk2.Value = True
これを削除したらいい感じになりました☆
(初心者)
それだとやっぱり↑の問題は解決出来てないはずだから、
'#####ここからテキストボックスが変わった時の処理
Private Sub txt1_Change()
Call CheckAndSettxt1
End Sub
Private Sub txt2_Change()
Call CheckAndSettxt1
End Sub
Private Sub txt3_Change()
Call CheckAndSettxt1
End Sub
Private Sub txt4_Change()
Call CheckAndSettxt1
End Sub
Private Sub txt5_Change()
CheckAndSettxt1
End Sub
Private Sub CheckAndSettxt1()
If txt2.Value <> "" And txt3.Value <> "" Then 'txt1,2が空白じゃなければ
If txt5.Value <> "" Then 'txt5が空白じゃなければ
If txt3.Value <> "" And txt4.Value <> "" Then 'txt3,4が空白じゃなければ
txt1.Value = "": chk1.Value = False
txt2.Value = "": chk2.Value = False
End If
ElseIf txt3.Value <> "" And txt4.Value <> "" Then 'txt5が空白且つ、3,4が空白じゃなければ
txt1.Value = "": chk1.Value = False
End If
End If
End Sub
'#####ここまでテキストボックスが変わった時の処理
ここまでしないとだめじゃないかなぁ
(稲葉)
Private Sub txt3_Change()
CheckAndSettxt1
End Sub
Private Sub txt4_Change()
CheckAndSettxt1
End Sub
Private Sub txt5_Change()
CheckAndSettxt1
End Sub
Private Sub CheckAndSettxt1()
If txt5.Value <> "" Then
If txt3.Value <> "" And txt4.Value <> "" Then
txt1.Value = "0": chk1.Value = False
txt2.Value = "0": chk2.Value = False
End If
ElseIf txt2.Value <> "" And txt3.Value <> "" And txt4.Value <> "" Then
txt1.Value = "0": chk1.Value = False
End If
End Sub
Private Sub chk4_Click()
If chk4.Value = True Then
txt4 = 400
Else
txt4 = ""
End If
End Sub
Private Sub chk3_Click()
If chk3.Value = True Then
txt3 = 300
Else
txt3 = ""
End If
End Sub
Private Sub chk1_Click()
If chk1.Value = True Then
txt1 = 100
Else
txt1 = ""
End If
End Sub
Private Sub chk2_Click()
If chk2.Value = True Then
txt2 = 200
Else
txt2 = ""
End If
End Sub
'#####ここまでチェックボックス制御#####
Private Sub cmb1_Click()
Dim t, c
Dim s As Long
With UserForm1
t = Array(.txt1, .txt2, .txt3, .txt4, txt5)
For Each c In t
s = s + Val(c.Value)
Next
.txtkei.Value = s
End With
End Sub
(初心者)
結局チェックボックスは単体で制御するのね(笑 (稲葉)
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.