简体   繁体   English

将单元格中输入的值添加到同一列中的另一个单元格

[英]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...对于此规则,您需要了解...

  1. When addressing cells, you can use their number or designation.寻址单元格时,您可以使用它们的编号或名称。 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")但字母更复杂,不能用于计算。
  2. All ranges have names.所有范围都有名称。 But in the absence of a given name VBA will concatenate a name from its first and last cells, like 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).您可以通过在名称创建中插入其他数字(例如您尝试过)来进一步复杂化这种连接。
  3. VBA defines a range by its first and last cells. VBA 通过其第一个和最后一个单元格定义范围。 Therefore VBA's preferred way to address Range("A1:D4") is 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.

相关问题 用于检查一列中某个单元格的值是否在另一列中某个单元格的范围内并在同一行上添加一个值的公式 - Formula to check if the value of a cell in a column is in the range of a cell in another column and add a value on the same line 如何根据另一个单元格更新您输入值的同一单元格? - How do you update the same cell you've entered a value in, based on another cell? Excel宏,如果在同一行的另一个单元格中输入了一个值,则可以更改单元格的对齐方式,边框和自动换行 - Excel Macro to change cell alignment, borders and wrap text if a value has been entered into another cell in the same row 根据在另一个单元格中输入的值返回单元格中的文本 - Return text in cell based on value entered in another cell 如果第 7 列中的一个单元格等于另一个单元格中的值,则更改为另一个单元格中的值 - If a cell in Column 7 equals the value in another cell change to the value in another cell 如果在另一列上找到一个单元格的值,如何从与同步单元格在同一行的另一列返回该单元格的值? - If a cell's value is found on another column, how to return the cell's value from another column which is on same row with the synced cell? 将单元格的最后一个值复制到另一列的单元格中 - copy last value of a cell into a cell of another column "将一个单元格的值添加到另一个单元格,然后将其清除" - add the value of a cell to another cell and then clear it 基于另一个单元格自动向单元格添加值 - Add a Value Automatically to a Cell Based on Another Cell 从与另一行中的单元格相对应的行中获取值(在同一列中) - Get value from a row that corresponds (in the same column) to a cell in another row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM