vba 单元格值更改时运行的宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15774685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Macro to run when cell value changes
提问by David Van der Vieren
I am realy not sure what the problem with this code is or if it is a problem with the formatting of my worksheet but the following code will not automatically run when the value of D8
changes:
我真的不确定这段代码的问题是什么,或者我的工作表的格式是否有问题,但是当值D8
更改时,以下代码不会自动运行:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$D" Then
Toggle_Rows
End If
End Sub
Is it a formating issue or a code issue?
是格式问题还是代码问题?
回答by user2140261
After seeing your question Here
在这里看到您的问题后
I'd like you to try this:
我想让你试试这个:
In the work book goto the sheet that you wish to change the value of D8 and have the code run. At the bottom of Excel right click the Tab with the name of that sheet and then select View Code
在工作簿中,转到要更改 D8 值的工作表并运行代码。在 Excel 底部右键单击带有该工作表名称的选项卡,然后选择查看代码
At the top of your Code window you should see the word "(General)".
在代码窗口的顶部,您应该会看到“(General)”这个词。
Click on the drop down and select "WorkSheet", you should see a new Sub called
单击下拉菜单并选择“WorkSheet”,您应该会看到一个名为的新 Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub
You can delete those lines. Now to the right of where you selected Worksheet theres another drop down this one should say "SelectionChange" Click on that then Select the word "Change" from the drop down. You should see another new sub
您可以删除这些行。现在,在您选择工作表的右侧,还有另一个下拉菜单,该下拉菜单应该是“SelectionChange”,单击它,然后从下拉列表中选择“Change”一词。你应该看到另一个新子
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
In between those 2 lines is where you want
在这两行之间是你想要的地方
If Target.Address = "$D" Then
Toggle_Rows
End If
回答by glh
Not an answer but a way to debug. as this works for me both manually and via code.
不是答案,而是一种调试方法。因为这对我手动和通过代码都有效。
Try one of the following to see the value you get:
尝试以下操作之一以查看您获得的值:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
Debug.Print Target.Address
If Target.Address = "$D" Then
Toggle_Rows
End If
End Sub