使用 If 语句清除单元格

Posted

技术标签:

【中文标题】使用 If 语句清除单元格【英文标题】:Using the If statement to clear a cell 【发布时间】:2018-02-11 21:52:06 【问题描述】:

我正在创建一个用户表单来输入地址数据。我有一个用于地址名称的文本框,用于选择道路、街道等的组合框,以及用于奇数从和到,偶数从和到的 4 个数字框。

我遇到的问题是让 VBA 清除“to”框的内容,如果数字已经输入到其中,但用户将“to”框更改为“to”框之后的更大值。

这是我目前拥有的代码:

Private Sub txtstreet_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case 65 To 90
    Case 97 To 122
    Case 32
    Case 127
    Case Else
      KeyAscii = 0
  End Select
End Sub

Private Sub UserForm_Initialize()
  roaddesciptor.List = Array("ROAD", "STREET", "WAY", "AVENUE", "DRIVE", "GROVE", "LANE", "GARDENS", "PLACE", "CRESENT", "CLOSE", "SQUARE", "HILL", "CIRCUS", "MEWS", "VALE", "RISE", "ROW", "MEAD", "WHARF")
  roaddesciptor.ListIndex = 0
End Sub

Private Sub txtaddoddfrom_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case 48 To 57
    Case Else
      KeyAscii = 0
  End Select
End Sub

Private Sub txtaddoddfrom_AfterUpdate()
  If txtaddoddfrom.Value < txtaddoddto.Value Or txtaddoddto = "" And txtaddoddfrom.Value Mod 2 = 1 Then Exit Sub
  If txtaddoddfrom.Value Mod 2 <> 1 Or txtaddoddfrom.Value > txtaddoddto.Value Then txtaddoddfrom = ""
  'Else: txtaddoddfrom = ""
End Sub

Private Sub txtaddoddto_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
         Case 48 To 57
         Case Else
            KeyAscii = 0
    End Select
End Sub

Private Sub txtaddoddto_AfterUpdate()
  If txtaddoddto.Value Mod 2 = 1 And txtaddoddto.Value >= txtaddoddfrom.Value Then Exit Sub
  If txtaddoddto.Value Mod 2 <> 1 Or txtaddoddto.Value < txtaddoddfrom.Value Then txtaddoddto = ""
End Sub

Private Sub txtaddevenfrom_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case 48 To 57
    Case Else
      KeyAscii = 0
  End Select
End Sub

Private Sub txtaddevenfrom_AfterUpdate()
  If txtaddevenfrom.Value < txtaddevento.Value Or txtaddevento = 0 And txtaddevenfrom.Value Mod 2 <> 1 Then Exit Sub
  If txtaddevenfrom.Value Mod 2 = 1 Or txtaddevenfrom.Value > txtaddevento.Value Then txtaddevenfrom = ""
End Sub

Private Sub txtaddevento_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
  Select Case KeyAscii
    Case 48 To 57
    Case Else
      KeyAscii = 0
    End Select
End Sub

Private Sub txtaddevento_AfterUpdate()
  If txtaddevento.Value Mod 2 <> 1 And txtaddevento.Value >= txtaddevenfrom.Value Then Exit Sub
  If txtaddevento.Value Mod 2 = 1 Or txtaddevento.Value < txtaddevenfrom.Value Then txtaddevento = ""
End Sub

【问题讨论】:

您可以通过使用If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0 检查数字输入来缩短您的代码。 ..... 也使用这种类型的语句使代码更清晰Case Asc("a") To Asc("z") .... case 127 行似乎被错误地包含了 【参考方案1】:

为了澄清,您想向“发件人”框的 AfterUpdate 事件添加代码,如果新的“发件人”框编号大于“收件人”框编号,则该代码将删除“收件人”框。

如果您将以下代码行添加到“来自”框的 AfterUpdate 事件中,它应该会执行您想要的操作。它与您已有的相同,只是您清除的是“发件人”框,而不是“收件人”框。

txtaddoddfrom.Value > txtaddoddto.Value Then txtaddoddto = ""

【讨论】:

【参考方案2】:

这里需要处理三个问题。

首先,您不能在字符串上使用数字运算符,这就是空白字段。有许多不同的方法可以实现这一点。一种方法是(0 &amp; "")。另一个是明确检查它们。我在调整您的代码时都使用了这两种方法。

其次,尝试更改您将要使用制表符进入的字段值失败。您必须将 Application.EnableEvents 语句包裹在尝试中才能成功。

最后,您在“from”处理程序中切换“from”字段而不是“to”字段。

这里有一对固定的处理程序:

Private Sub txtaddoddfrom_AfterUpdate()
  If (0 & txtaddoddfrom) Mod 2 <> 1 Then txtaddoddfrom = "" ' If even (or ""), zap it
  If txtaddoddfrom = "" Or txtaddoddto = "" Then Exit Sub 'If was even or blank, done -> exit
  Application.EnableEvents = False
  If txtaddoddto < txtaddoddfrom Then txtaddoddto.Value = "" ' Otherwise compare and zap txtaddoddto if smaller
  Application.EnableEvents = True
End Sub


Private Sub txtaddoddto_AfterUpdate()
  If (0 & txtaddoddto) Mod 2 <> 1 Then txtaddoddto = "" ' If even (or ""), zap it
  If txtaddoddto = "" Or txtaddoddfrom = "" Then Exit Sub 'If was even or blank, done -> exit
  Application.EnableEvents = False
  If txtaddoddfrom > txtaddoddto Then txtaddoddfrom = "" ' Otherwise compare and zap txtaddoddfrom if greater
  Application.EnableEvents = True
End Sub

注意:可以使用.Value 显式访问字段值,也可以不使用它隐式访问。始终使用一种或另一种形式是个好主意。

【讨论】:

以上是关于使用 If 语句清除单元格的主要内容,如果未能解决你的问题,请参考以下文章

如何在摇摆中的假if语句后连续闪烁表格单元格

C# Winforms DataGridView 选定单元格正在清除单元格值

选择您的 if 语句触发的单元格并在 vba 中选择该单元格

如何根据 if 语句更新单元格?

IF 语句:如果条件为假,如何将单元格留空(“”不起作用)

嵌套的 if else 语句无法通过目标单元格工作