【问题标题】:VBA Change value in textbox based on whether other textboxes are emptyVBA根据其他文本框是否为空更改文本框中的值
【发布时间】:2019-03-11 23:29:03
【问题描述】:

在这里遇到一些代码,寻求帮助。我试图找出根据另一个文本框是否有值来更新文本框值的正确语法。

故障

  • Textbox1 名称 = arisk_box
  • Textbox2 名称 = adjust_box3
  • Textbox3 名称 = rr_box

这是我目前所拥有的:

Private Sub arisk_box_Change()
If arisk_box.Text <> "" And adjust_box3.Text = "" Then
    rr_box.Value = arisk_box.Value
 ElseIf arisk_box.Text <> "" And adjust_box3.Text <> "" Then
    rr_box.Value = adjust_box3.Value
 End If
 End Sub

   *If arisk_box has a *VALUE* and adjust_box3 *DOES NOT* then rr_box = the value from arisk_box
  Elseif arisk_box AND adjust_box3 both have a *VALUE* then rr_box = the value from adjust_box3

【问题讨论】:

    标签: vba excel textbox is-empty


    【解决方案1】:

    最简单的方法是使用 2 个单独的宏,一个用于每个文本框更改事件。但它可能不会像你想要的那样顺利。我相信有人会有更先进的方法。

    Private Sub adjust_box3_Change()
        If arisk_box <> "" And adjust_box3 = "" Then rr_box = arisk_box
    End Sub
    
    Private Sub arisk_box_Change()
        If arisk_box <> "" And adjust_box3 <> "" Then rr_box = adjust_box3
    End Sub
    

    【讨论】:

    • 您好 GMalc,感谢您的回复,但这不起作用。我正在尝试将相同的代码移动到其他东西下,看看它是否根本没有做任何事情,因为如果位置。
    • 代码已根据您的要求进行了测试和执行。它是如何不起作用的?你把潜艇放在用户窗体中了吗? (潜艇必须是用户窗体的一部分才能工作)。如果 subs 在 UserForm 中,您是否真的更改了任一文本框中的值?
    • 嗨 GMalc,代码的第一行有效,但其余部分无效。正如您在上面编写的那样,该代码位于文本框 Subs 下的用户表单中。澄清一下,arisk_box 总是会被填写,并且会先被填写。 adjust_box3 是一个变量,意思是,它有时会被填或不填,它总是会填“秒”到arisk_box。因此,当 arisk_box 被填充时,rr_box 被填充,但是当我用字符串填充 adjust_box3 时,rr_box 不会将自身更新为 adjust_box3 中的字符串。它仍然停留在 arisk_box 上。这是否更有帮助?
    • 澄清...如果 arisk_box = true 并且 adjust_box3 = False 那么 rr_box = arisk_Box --- 否则如果 arisk_box = true 并且 adjust_box3 = True 那么 rr_box = adjust_box3。如果 arisk_box 在 adjst_box 已经填满的情况下更新了怎么办?
    • 抱歉误会,我想我的更新是你想要的。
    猜你喜欢
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 2020-12-26
    • 1970-01-01
    相关资源
    最近更新 更多