我在寻找一些帮助如何根据单元格的值触发宏的继续。如何根据单元格内容运行宏
我希望的是,当单元格A1 =单元格B1时,我需要它执行任务,否则结束脚本。
我认为这将是一个简单的If... Then
脚本?任何援助非常感谢!
我在寻找一些帮助如何根据单元格的值触发宏的继续。如何根据单元格内容运行宏
我希望的是,当单元格A1 =单元格B1时,我需要它执行任务,否则结束脚本。
我认为这将是一个简单的If... Then
脚本?任何援助非常感谢!
这里有一种方法可以完成,这是假设你的所有值已经存在,并且你正在运行一个支票,而不是某人在现场打字,在这种情况下,约翰将是一个很好的选择。然而,我不喜欢关于worksheet_change的一件事是,一旦宏被执行,就不能再次撤消。如果长度> 0,我添加了,就好像你有两个空单元格,它仍然会触发。
Sub TestValues()
'Define variables
Dim rng As Range
Dim cell As Range
'Set variables
Set rng = Range("A1:A10")
'Begin
For Each cell In rng
If Len(cell) > 0 Then
If cell.Value = cell.Offset(0, 1).Value Then
'Run macro
End If
End If
Next cell
End Sub
*编辑 - 我想我没有完全读它的权利。出于某种原因,我认为你有一系列的价值观需要经历。做你问什么它会更喜欢这个,对不起,长期职位...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$1" Then
If Len(Target) > 0 Then
If Target.Value = Target.Offset(0, 1).Value Then
'Run macro
End If
End If
End If
End Sub
我假设你的宏负责更换A1和/或B1细胞,使其真正知道的当做那个“继续检查”。
在这种情况下,你可以做,以多种方式,这里有两个:
保持“延续”内宏
Sub main()
' your code till the statement that changes A1 and/or B1
If Range("A1").Value <> Range("B1").Value Then Exit Sub '<-- if cells A1 and B1 DON'T have the same values then exit sub
'here follows your code for the "Task"
End Sub
代码的需求任务到另一个Sub
Sub main()
' your code till the statement that changes A1 and/or B1
If Range("A1").Value = Range("B1").Value Then DoTask '<-- if cells A1 and B1 HAVE the same values then call 'DoTask()'
End Sub
Sub DoTask()
' here your code code for the "Task"
End Sub
在后一种情况下,您可能需要将o NE(或更多)的“主”宏DoTask
Sub main()
' your code till the statement that changes A1 and/or B1
If Range("A1").Value = Range("B1").Value Then DoTask Range("A1").Value '<-- if cells A1 and B1 HAVE the same values then call 'DoTask passing A1 cell value as "argument"
End Sub
Sub DoTask(val As Variant) '<--| DoTask accepts a parameter declared as of Variant type (but you may choose a more proper one
' here your code code for the "Task"
' it will use 'val'
End Sub
@kitarika,你通过了吗? – user3598756
这种事情参数通常被写为'Worksheet_Change'事件的事件处理程序中完成。在网上很容易找到各种例子。 –