vba 将单元格相互连接起来;让单元格更新链接的单元格,反之亦然
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19531485/
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
Linking cells to each other; having cells update linked cells and vice versa
提问by Clayton J Roberts
I have two ideas that could lead to more or less the same result.
我有两个想法可能会导致或多或少相同的结果。
I am trying to have similar cells or tables update themselves to whatever the most recent entry was in the linked system. For example, cell A1 is linked to cell B2 (in this system that I am trying to create). I would enter something like "1" or "Text" or whatever in A1 and the system would update cell B2 to whatever I entered in cell A1. However the opposite must work in the same manner. If I changed whatever in cell B2 to, say, "5", cell A1 would also display "5". Also, note that I have Excel 2013.
我试图让类似的单元格或表格将自己更新为链接系统中的最新条目。例如,单元格 A1 链接到单元格 B2(在我试图创建的这个系统中)。我会在 A1 中输入诸如“1”或“文本”之类的内容,系统会将单元格 B2 更新为我在单元格 A1 中输入的任何内容。然而,相反的必须以相同的方式工作。如果我将单元格 B2 中的任何内容更改为“5”,单元格 A1 也会显示“5”。另外,请注意我有 Excel 2013。
I need this to work with a cell or a table. So, getting to the possible solutions...
我需要它来处理单元格或表格。因此,获得可能的解决方案......
A subroutine in VBA that automatically updates all the linked cells or tables.
Some sort of mechanic unknown to me that uses VBA or another Excel aspect to do this, like a function or tool.
VBA 中的一个子程序,它自动更新所有链接的单元格或表格。
某种我不知道的使用 VBA 或其他 Excel 方面来执行此操作的机制,例如函数或工具。
In your answer or solution, please be mindful to my inexperience with VBA. Thanks in advance.
在您的回答或解决方案中,请注意我对 VBA 的经验不足。提前致谢。
回答by henderso
You can try a worksheet change function:
您可以尝试工作表更改功能:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("A1").Value
ElseIf Target.Address = Range("B1").Address then
ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value
End If
End Sub
Although this seems like it might create an infinite loop (the update from the change causes another change) it DOES work for me in Excel 2010..
尽管这似乎可能会创建一个无限循环(更改的更新会导致另一个更改),但它在 Excel 2010 中确实对我有用..
There are other Worksheet functions you can try as well (e.g. Worksheet_SelectionChange)
您也可以尝试其他工作表功能(例如 Worksheet_SelectionChange)
This macro needs to be placed/entered as a WORKSHEET macro on the sheet where you desire to use it..it will not work in a Module.
此宏需要作为工作表宏放置/输入到您希望使用它的工作表上。它在模块中不起作用。
To install:
安装:
1) Save your workbook as a macro-enabled file.
1) 将您的工作簿另存为启用宏的文件。
2) Close Excel, reopen file, and enable macro security
2) 关闭 Excel,重新打开文件,并启用宏安全
3) Type Alt-F11
3) 键入 Alt-F11
4) In the project explorer view on the left, look for your sheet name. Double click it
4) 在左侧的项目资源管理器视图中,查找您的工作表名称。双击它
5) In the code entry area on right (big window) paste the example code above
5)在右侧的代码输入区(大窗口)粘贴上面的示例代码
6) Return to your worksheet and try it.
6) 返回到您的工作表并尝试一下。