【问题标题】:Excel increment own value (circular reference)Excel自增值(循环引用)
【发布时间】:2015-04-02 11:06:18
【问题描述】:

我正在制作一张如下所示的桌子:

Last Type | Today Type | N Days
-------------------------------
  Type1   |   Type2    |   0
-------------------------------
  Type1   |   Type1    |   1
-------------------------------
  Type2   |   Type2    |   20   
-------------------------------

每天都会在“今日类型”中使用相应的类型更新表格。 “N Days”列将计算最后一种类型的天数。

【问题讨论】:

标签: excel counter circular-reference


【解决方案1】:

您可以在 excel 中使用迭代计算选项,但我不是这个的忠实粉丝,对我来说,这项任务似乎非常适合使用 Worksheet_change 事件处理程序。

尝试在您的工作表代码中插入此类代码 - 不是标准模块(因此右键单击工作表名称选项卡并 ->显示代码):

Private Sub Worksheet_Change(ByVal Target As Range)
Dim mycell As Range
If Not Intersect(Target, Columns(2)) Is Nothing Then
  For Each mycell In Intersect(Target, Columns(2))
    If mycell.Value = mycell.Offset(0, -1).Value Then
      mycell.Offset(0, 1).Value = mycell.Offset(0, 1).Value + 1
    Else
      mycell.Offset(0, 1).Value = 0
      mycell.Offset(0, -1).Value = mycell.Value
    End If
  Next mycell
End If
End Sub

记得将工作簿保存为包含宏的格式,例如 xlsm 或 xls 上面的代码适用于 A 列和 B 列(以前和当前)中的数据以及 C 中的计数器。在现实生活中,最好使用 Range("B2:B10") 或类似的而不是 Columns(2) (它出现两次在上面的代码中)。

【讨论】:

  • 谢谢卡珀!这正是我需要的!!! :) 我没有足够的代表来投票抱歉......
  • 没问题,我很高兴它成功了。如果您愿意,您可以在 excelforum.com 上检查声誉可能性(顺便说一下,在那里发布附件很容易) - 请参阅我的个人资料:excelforum.com/members/682378.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多