Excel VBA - 基于一个单元格中的更改进行多个单元格更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/750361/
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
Excel VBA - Multiple cell update based on a change in ONE cell
提问by RedBlueThing
I want to fill a range of cell in Excel, when another one changes.
当另一个单元格发生变化时,我想在 Excel 中填充一系列单元格。
I have a macro which can retrieve customer details from another master workbook (WB2), based on the project number. Project numbers are in WB1 as a list. As and when user selects a project number from WB1, I need to fill cells in the range of H9:H15 (in WB1) with customer details. I am using worksheet_change event to trigger this.
我有一个宏,它可以根据项目编号从另一个主工作簿 (WB2) 中检索客户详细信息。项目编号在 WB1 中作为列表。当用户从 WB1 中选择项目编号时,我需要用客户详细信息填充 H9:H15(在 WB1 中)范围内的单元格。我正在使用 worksheet_change 事件来触发这个。
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A" Then
Call modInvoiceFiller.FillCustomerDetails
End If
End Sub
With this code, no update happens. If I run the procedure manually, it fills in the required details. Could someone please help?
使用此代码,不会发生更新。如果我手动运行该过程,它会填写所需的详细信息。有人可以帮忙吗?
Regards, Prabhu
问候, 帕布
回答by paxdiablo
That works fine for me. Are you sure the macro is attached to the right worksheet rather than the workbook (or worse, in a module)?
这对我来说很好用。您确定宏附加到正确的工作表而不是工作簿(或更糟的是,在模块中)?
An easy way to tell is to change it to:
一个简单的判断方法是将其更改为:
Private Sub Worksheet_Change(ByVal Target As Range)
MsgBox Target.Address
If Target.Address = "$A" Then
MsgBox "XX " & Target.Address
End If
End Sub
and make sure a message pops up when you change any cell (including the value you should be checking for in your if statement), and also make sure you get two messages when you change cell A15.
并确保在更改任何单元格时弹出一条消息(包括您应该在 if 语句中检查的值),并确保在更改单元格 A15 时收到两条消息。
回答by RedBlueThing
A couple of things to check:
需要检查的几件事:
- Have you implemented Worksheet_Change on the Sheet that contains the project numbers?
- If you remove your modInvoiceFiller.FillCustomerDetails code and replace it with a msgbox call, does the code get executed?
- 您是否在包含项目编号的工作表上实施了 Worksheet_Change?
- 如果您删除 modInvoiceFiller.FillCustomerDetails 代码并将其替换为 msgbox 调用,代码是否会被执行?