【发布时间】:2017-12-16 12:32:27
【问题描述】:
我正在尝试执行一个 if 语句,该语句仅在某个单元格包含特定文本时才激活。有问题的单元格需要根据将要更改的整数动态更改,到目前为止,我尝试了多种方法,但似乎没有任何效果。
If Cells(counter, 1).text = "text" then
If Cells(counter, 1).value = "text" then
If Range(Cells(counter, 1)).text = "text then
If Range(Cells(counter, 1)).value = "text then
这似乎是一个简单的过程,有人有解决方案吗?
谢谢,孢子
编辑:
Private Sub CheckBox_Change()
If CheckBox.Value = True Then
'do stuff
End If
ElseIf CheckBox.Value = False Then
If Cells(1, counter).Value = "text1" Or Cells(1, counter).Value =
"text2" Or Cells(1, counter).Value = "text3" Then
'do stuff
End If
End If
End Sub
这是我收到错误消息“应用程序定义或对象定义错误”的地方。
编辑 2: 问题是我试图在几个不同的 subs 中调用计数器,它不是一个公共整数。感谢您的帮助!
【问题讨论】:
-
代码
If Cells(i, 1).value = "text" Then应该可以工作。但是,您应该发布更多代码,因为不清楚是什么使代码运行。它是手动执行的,还是应该在 Worksheet_Change 等上运行? -
好的,所以当我激活工作表时它可以工作,但是当复选框更改为 false 时它不起作用。唯一的区别是我在 IF 语句中有多个语句,如: If Cells(i, 1).value = "text1" or Cells(i, 1).value = "text2" then
-
尝试发布更多代码。该错误并非源自您发布的
If声明。 -
Private Sub UT_Change() If UT.Value = True Then 'do stuff End If ElseIf UT.Value = False Then If Cells(1, counter).Value = "text1" Or Cells(1, counter).Value = "text2" Or Cells(1, counter).Value = "text3" Then 'do stuff End If End If End Sub抱歉,格式似乎不正确 -
尝试编辑您的原始帖子,而不是将其作为评论发布。您还应该指定
UT.Value是什么。
标签: vba if-statement text range cells