我在excel 2007文件中有2张sheet1和sheet2。
在sheet2中,我有一个由表单/宏(具有树视图控件)管理的列。选择一个元素后,单元格将填充“x”,如果未选中该单元格,则单元格将填充“”(无)。
在sheet1中我想创建一个等于sheet2列的列。 所以例如:如果sheet2!C24 =“x”那么sheet1!c24也应该是“x” 我也希望它能同时发挥作用。如果用户将sheet1!c24更改为“x”,那么我希望sheet2!c24采用相同的值。
问题: - 在Sheet1中,我尝试了sheet1!c24 = sheet2!c24,但是当sheet2!c24 =“”时,sheet1!c24显示0而不是没有 - 在Sheet2中,我尝试了sheet2!c24 = sheet1!c24,但随后单元格显示公式(='sheet1!c24')而不是值...
所以基本上,我想要的是无论你做什么改变,在sheet1或sheet2中,sheet1和sheet2中的两列都会更新...... 我怎样才能做到这一点?
答案 0 :(得分:1)
我认为您需要做的是对两个工作表使用Worksheet_Change
事件,如果您感兴趣的列中进行了更改,则更新另一个工作表中的相同单元格。
这样的东西将放在工作表代码模块中:
Private Sub worksheet_change(ByVal target As Range)
Dim c As Range
'Test to see if the cell just changed is
'in the column we are interested in
Set c = Application.Intersect(target, Range("A:A"))
If Not c Is Nothing Then
'Copy across to other sheet
If Not beingEdited Then
beingEdited = True
Sheet1.Range(target.Address) = target.Value
beingEdited = False
End If
End If
End Sub
您需要在具有更大范围的其他地方声明beingEdited
变量,以便您可以避免事件触发自身并且Excel陷入循环。
在另一张表格中,您基本上使用相同的程序,但它会引用第一张工作表,例如Sheet1.Range(target.Address) = target.Value
。
显然,你必须将它调整到你的范围/表。
答案 1 :(得分:1)
你有正确的想法,但你可能需要在做出改变之前关闭事件,否则你将最终陷入循环
Private Sub worksheet_change(ByVal target As Range)
application.enableevents = false
sheet1.range("c24").value = sheet2.("c24").value
application.enableevents = true
end sub
请确保最后再次启用事件。
答案 2 :(得分:0)
我做了类似的事情,我有一个摘要表和一份测试表。当我在测试表中添加一个新值并且它通过(P)时,摘要表中的单元格将保持增量。这是为了计算通过的测试数量。这是:
COUNTIF(测试!$ C $ 5:$ C $ 1017,“P”);
希望这会有所帮助。