vba - VBA - 如何编写具有两个条件的 if 语句
问题描述
我正在尝试编写一个简短的 vba,以说明 A 列是否为数字(非空)且 B 列是否为文本-“已关闭”,C 列返回为当前日期。我在下面使用的代码似乎不起作用。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
If Cell.Column = Range("A:B").Column Then
If Cells(Cell.Row, "A").Value <> "" And Cells(Cell.Row, "B").Value = "closed" Then
Cells(Cell.Row, "C").Value = Int(Now)
Else
Cells(Cell.Row, "C").Value = ""
End If
End If
Next Cell
End Sub
有什么帮助吗?
解决方案
你能试试这个吗?我认为您检查该列的语法已关闭。Range("A:B").Column
将返回 1。
此外,这会显式检查 A 列中的数字,而不仅仅是非空白。
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Cell As Range
For Each Cell In Target
If Cell.Column = 1 Or Cell.Column = 2 Then
If Len(Cells(Cell.Row, "A").Value) > 0 And IsNumeric(Cells(Cell.Row, "A").Value) And Cells(Cell.Row, "B").Value = "closed" Then
Cells(Cell.Row, "C").Value = Int(Now)
Else
Cells(Cell.Row, "C").Value = vbNullString
End If
End If
Next Cell
End Sub
推荐阅读
- angular - Angular [ngClass] 将一行 if 语句转换为多个 if
- vector - 如何从包含 Julia 范围的向量中获取值
- filter - 从 Makefile 变量中提取模式和单词
- c++ - 继承中的函数重载歧义,其中函数具有相同数量的参数
- r - 如何绘制逻辑回归
- java - 使用java从字符串中提取html部分(包括纯文本和html部分)
- string - 将 int 值附加到字符串
- graphviz - Set pre-defined node styles?
- python - 将代码从 Python Turtle 转换为 Tkinter
- typescript - 打字稿 - 在没有参数的函数上需要泛型类型