【问题标题】:how loop if function in VBA如何在VBA中循环if函数
【发布时间】:2017-06-21 17:32:46
【问题描述】:

如何在 vba 代码中重复 if 函数 大家好,我在表单上使用 if 创建了一个命令行。但是我一遍又一遍地写它有困难。有没有适当的方法来简化这篇文章?

If UserForm1.checkbox15.Value = True Then
UserForm2.textbox1.enable = True
UserForm2.textbox1.BackColor = RGB(200, 200, 200)
Else
UserForm2.textbox1.enable = True
UserForm2.textbox1.BackColor = RGB(255, 255, 255)
End If

If UserForm1.checkbox16.Value = True Then
UserForm2.textbox2.enable = True
UserForm2.textbox2.BackColor = RGB(200, 200, 200)
Else
UserForm2.textbox2.enable = True
UserForm2.textbox2.BackColor = RGB(255, 255, 255)
End If

If UserForm1.checkbox17.Value = True Then
UserForm2.textbox3.enable = True
UserForm2.textbox3.BackColor = RGB(200, 200, 200)
Else
UserForm2.textbox3.enable = True
UserForm2.textbox3.BackColor = RGB(255, 255, 255)
End If

If UserForm1.checkbox18.Value = True Then
UserForm2.textbox4.enable = True
UserForm2.textbox4.BackColor = RGB(200, 200, 200)
Else
UserForm2.textbox4.enable = True
UserForm2.textbox4.BackColor = RGB(255, 255, 255)
End If

.... .... .... 到 textbox660 ...

【问题讨论】:

  • 这是不可读的,请格式化您的代码以便于理解。
  • 我打算为你格式化你的代码,直到我看到你决定在这里粘贴的未格式化的混乱。请edit您的帖子并至少努力正确格式化。事实上,这不过是代码转储混乱。
  • 抱歉,我无法发布代码。连续拒绝。我在记事本++中编写的代码,然后我将其粘贴到浏览器中。每次我编辑时都会以某种方式被拒绝..
  • 我要冒昧地提出建议,如果您有一个包含 660 个复选框(并且可能有很多文本框)的表单,您可能需要完全重新设计应用程序。 可能 这样做(例如,通过参数化函数或子例程等),但你在这里尝试做什么不是很清楚......
  • 我创建了一个映射,其中 1 行代表 1 个主复选框。每行有 22 个复选框。总共有 30 行主复选框。当我尝试时,结果证明应用程序运行顺利。然后我制作另一个表格,用于从之前的表格中获取值。它只是一个复选框,我将其替换为一个文本框,该文本框将填写映射的书面值。所以如果包含该复选框的表单为真,那么下面表单上的文本框可用于值的内容...

标签: vba loops iif-function


【解决方案1】:

在模块顶部的子例程之外将您的颜色长值定义为Const

Public Const bgColorTrue As Long = 13158600  '## RGB(200,200,200)
Public Const bgColorFalse As Long = 16777215 '## RGB(255,255,255)

然后你可以做这样的事情,这稍微简化了一些,但仍然需要枚举所有的 TextBox + CheckBox 对。

UserForm2.textbox1.BackColor = IIF(UserForm1.checkbox15.Value = True, bgColorTrue, bgColorFalse)
UserForm2.textbox2.BackColor = IIF(UserForm1.checkbox16.Value = True, bgColorTrue, bgColorFalse)
UserForm2.textbox3.BackColor = IIF(UserForm1.checkbox17.Value = True, bgColorTrue, bgColorFalse)
UserForm2.textbox4.BackColor = IIF(UserForm1.checkbox18.Value = True, bgColorTrue, bgColorFalse)
UserForm2.textbox5.BackColor = IIF(UserForm1.checkbox19.Value = True, bgColorTrue, bgColorFalse)
' etc...

或者(假设复选框和文本框的索引之间的算术关系是恒定的),您可以做一个循环并创建一个子程序来识别哪些TextBox与哪些CheckBox配对:

Dim cb as MSForms.CheckBox, ctrl as Control
For Each ctrl in UserForm1.Controls
    If TypeName(cb) = "CheckBox" Then
        Call UpdateForm2(cb, UserForm2)
    End If
Next

使用类似的子类:

Sub UpdateForm2(ByRef cb as MSForms.CheckBox, byRef Uf as UserForm)
    Dim tb as MSForms.TextBox
    Dim iCB as Long, iTB as Long
    '## Get the index of this CheckBox name:
    iCB = CLng(Replace(cb.Name, "checkbox", vbNullString))
    '## Compute the index of corresponding TextBox name
    iTB = iCB - 14
    '## Handle the TextBox on the other UserForm
    Set tb = Uf.Controls("TextBox" & iTB)
    tb.BackColor = IIF(cb.Value = True, bgColorTrue, bgColorFalse)
End Sub

【讨论】:

  • 哦,不好意思,我写错了vba代码,应该是: If UserForm1.checkbox15.Value = True Then UserForm2.textbox1.Locked = False UserForm2.textbox1.BackColor = RGB(255, 255, 255) Else UserForm2.textbox1.Locked = True UserForm2.textbox1.BackColor = RGB(200, 200, 200) End If ... etc 我已经测试过并且成功了。只是您忘记添加启用文本框功能的命令。但是你的建议很中肯,我可以申请。谢谢大家
猜你喜欢
  • 2019-05-16
  • 2015-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-03
  • 2017-07-22
  • 2021-07-30
  • 1970-01-01
相关资源
最近更新 更多