[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]
『似たような処理をスマートに書くには』(ナッツ)
昨日に引き続き質問失礼します。
2つのテキストボックスに入力した文字(.ex個,kg,L)を合体させてラベルに表示したいと思い、以下のコードを書きました。
似たような処理を続けているため、Controlsコレクションを使用してまとめてみたのですが、それでも処理の数だけprivate sub...private sub...と並んでいるので、
Private Sub Controls("TextBox" & 1 to 5)_Change
みたいな処理はできないのかなぁと思い質問させていただきました。回答いただけると幸いです。
よろしくお願いします。
Private Sub TextBox1_Change()
If TextBox1.Value <> "" Then
If TextBox2.Value <> "" Then Label1.Caption = TextBox1.Value & "/" & TextBox2.Value Else Label1.Caption = TextBox1.Value & "/" & "入荷単位" End If Else If TextBox2.Value <> "" Then Label1.Caption = "在庫単位" & "/" & TextBox2.Value Else Label1.Caption = "在庫単位" & "/" & "入荷単位" End If End If End Sub
Private Sub TextBox2_Change()
If TextBox1.Value <> "" Then
If TextBox2.Value <> "" Then Label1.Caption = TextBox1.Value & "/" & TextBox2.Value Else Label1.Caption = TextBox1.Value & "/" & "入荷単位" End If Else If TextBox2.Value <> "" Then Label1.Caption = "在庫単位" & "/" & TextBox2.Value Else Label1.Caption = "在庫単位" & "/" & "入荷単位" End If End If End Sub
Private Sub TextBox3_Change()
If TextBox3.Value <> "" Then
If TextBox4.Value <> "" Then Label2.Caption = TextBox3.Value & "/" & TextBox4.Value Else Label2.Caption = TextBox3.Value & "/" & "入荷単位" End If Else If TextBox3.Value <> "" Then Label2.Caption = "在庫単位" & "/" & TextBox4.Value Else Label2.Caption = "在庫単位" & "/" & "入荷単位" End If End If End Sub
・ ・ ・
< 使用 Excel:Excel2007、使用 OS:Windows7 >
Classを使うことになると思います。 参考過去ログです。
[[20200607100518]] 『ユーザーフォーム イベントの共通化について』(かずま)
(OK) 2021/01/27(水) 13:43
これに規則性はありますか?
TextBox1とTextBox2はTextBox1がブランクでない場合 TextBox3はTextBox3がブランクでない場合
となってますが。
Private Sub TextBox1_Change() If TextBox1.Value <> "" Then
Private Sub TextBox2_Change() If TextBox1.Value <> "" Then
Private Sub TextBox3_Change() If TextBox3.Value <> "" Then
(OK) 2021/01/27(水) 13:59
こんなかんじ
'------------ フォームモジュール ----------------------------------- Dim clsTxtEvent() As Class1
Private Sub UserForm_Initialize() ReDim clsTxtEvent(1 To 2) For i = 1 To 2 Set clsTxtEvent(i) = New Class1 Set clsTxtEvent(i).tbZaiko = Me.Controls("TextBox" & i * 2 - 1) Set clsTxtEvent(i).tbNyuka = Me.Controls("TextBox" & i * 2) Set clsTxtEvent(i).lbTanni = Me.Controls("Label" & i) Next End Sub
'---------- クラスモジュール Class1 ----------------------- Public WithEvents tbZaiko As MSForms.TextBox Public WithEvents tbNyuka As MSForms.TextBox Public lbTanni As MSForms.Label
Private Sub tbZaiko_Change() Me.lbTanni.Caption = IIf(tbZaiko.Value = "", "在庫単位", tbZaiko.Value) & "/" & _ IIf(tbNyuka.Value = "", "入荷単位", tbNyuka.Value) End Sub
Private Sub tbNyuka_Change() Me.lbTanni.Caption = IIf(tbZaiko.Value = "", "在庫単位", tbZaiko.Value) & "/" & _ IIf(tbNyuka.Value = "", "入荷単位", tbNyuka.Value) End Sub (´・ω・`) 2021/01/27(水) 14:56
[ 一覧(最新更新順) ]
YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki.
Modified by kazu.