[英]Add value entered in cell to another cell in the same column
I want to add the value entered in a cell in a certain row to another cell in that same column我想将在某一行的单元格中输入的值添加到同一列中的另一个单元格
Example:例子:
If I enter a value in cell B12, add that value to the value in B17.如果我在单元格 B12 中输入一个值,将该值添加到 B17 中的值。
If I enter a value in cell D12, add that value to the value in D17 and so on.如果我在单元格 D12 中输入一个值,将该值添加到 D17 中的值,依此类推。
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Target.Row = 12 Then
Range(Target.Column & 17).Value = Range(Target.Column & 17).Value + Target.Value
End If
Application.EnableEvents = True
End Sub
Problem is, that Target.Column is a integer and not a Character.问题是,Target.Column 是 integer 而不是字符。 So the Code will not run, since Range expects a char as column and not an integer (at least that is what I think the debugger wants to tell me).所以代码不会运行,因为 Range 需要一个 char 作为列,而不是 integer(至少我认为调试器想要告诉我的是)。
Greetings, Maverick问候,特立独行
The presumption that Column must be a letter is mistaken. Column 必须是字母的假设是错误的。 To the contrary, Column is a number which, for users' convenience (not Excel's) is displayed as a letter in some usages.相反,Column 是一个数字,为了方便用户(不是 Excel),在某些用法中显示为字母。 Therefore this code will work.因此,此代码将起作用。
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
If .Row = 12 Then
Application.EnableEvents = False
Cells(17, .Column).Value = .Value
Application.EnableEvents = True
End If
End With
End Sub
A rule you might go by is to address individual cells using the syntax for addressing cells, which is Cells([Row], [Column]) and use range names only when addressing ranges comprising more than one cell.您可能会遵循 go 的一条规则是使用用于寻址单元格的语法来寻址单个单元格,即Cells([Row], [Column])并且仅在寻址包含多个单元格的范围时才使用范围名称。 For this rule you need to understand that...对于此规则,您需要了解...
Cells(3, 2)
may also be written as Cells(3, "B")
but the letter is more complicated and can't be used in calculations. Cells(3, 2)
也可以写成Cells(3, "B")
但字母更复杂,不能用于计算。Range("A1:D4")
.但是在没有给定名称的情况下, VBA 将从其第一个和最后一个单元格连接一个名称,例如Range("A1:D4")
。 You can further complicate this concatenation by interjecting other numbers into the name creation (such as you tried).您可以通过在名称创建中插入其他数字(例如您尝试过)来进一步复杂化这种连接。Range(Cells(1, 1), Cells(4, 4))
.因此,VBA 处理Range("A1:D4")的首选方式是Range(Cells(1, 1), Cells(4, 4))
。 That looks effusive but if you are in a position where you have to calculate any of the coordinates you will find that it's the simplest method.这看起来很热情,但如果你在 position 中,你必须计算任何坐标,你会发现这是最简单的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.