简体   繁体   English

将一列中所有单元格的值添加到另一列中的相应单元格,然后清除原始单元格

[英]Add value in all cells within a column to corresponding cell in another column and then clear original cells

I need to create an Excel document with updating totals in the A column, based on numbers entered in the B column. 我需要根据B列中输入的数字创建一个Excel文档,并在A列中更新总计。 Each respective cell in row A should update based on its equivalent B cell value whenever a new value is added, and then the value entered into B is cleared once added to A. 每当添加新值时,行A中的每个相应单元格都应基于其等效的B单元格值进行更新,然后,一旦添加到A中,就清除输入到B中的值。

I have gotten things working for one single row but don't have knowledge or understanding on how to best make this work for EACH cell pair in the entire column. 我已经完成了单行工作,但是没有知识或了解如何最好地使整列中的每个单元对都有效。 I really don't want to copy and paste this 10,000 times and update the cells to reference the correct pair. 我真的不想复制并粘贴此10,000次并更新单元格以引用正确的对。 Code for single cell: 单个单元格的代码:

Private bIgnoreEvent As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
    If bIgnoreEvent Then Exit Sub
    bIgnoreEvent = True
    Cells(1, 2) = Cells(1, 2) + Cells(1, 1)
    Cells(1, 1) = ""
    bIgnoreEvent = False
End Sub

I am hoping this can be achieved with a loop function, or a range of some sort. 我希望可以通过循环功能或某种范围的方法来实现。

This should work: 这应该工作:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range, c As Range

    'any updates in ColB?
    Set rng = Application.Intersect(Target, Me.Columns(2))

    If Not rng Is Nothing Then
        Application.EnableEvents = False '<< prevent re-triggering of event
        'Process each updated cell
        For Each c In rng
            If IsNumeric(c.Value) Then
                With c.Offset(0, -1)
                    .Value = .Value + c.Value
                End With
                c.ClearContents
            End If
        Next c
        Application.EnableEvents = True  '<< re-enable event
    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 清除列中所有等于“”的单元格 - Clear all cells in column who = “”? 将值添加到所有工作表中列中的所有单元格 - Add a value to all cells in a column in all worksheets 将一列所有单元格的值移动到对应的excel行号 - Move the value of all cells in a column to their corresponding excel row number 如何比较多个命名列范围中相应单元格的值并更改另一个相应单元格中的内部颜色 - How to compare values of corresponding cells in multiple named column ranges and change interior color in another corresponding cell 如果一个单元格等于条件,则查找范围内的单元格将其值复制到另一列 - Find cells within a range if one cell equals a criteria copy its value to another column 根据另一列中单元格的值突出显示单元格 - Highlight cells based on the value of cells in another column 如何在当前单元格的位置之前添加*在另一个工作表中*的列中的所有单元格? - How to add all cells in a column *in another sheet* before location of current cell? Excel总和列的所有单元格匹配单元格条件的值 - Excel sum column's value of matching cell criteria for all the cells 更改列中所有单元格的值 - Change value of all cells in a column 清除宏可清除特定列中空白单元格上特定范围的单元格 - Clear Macro to clear specific range of cells on Blank cell in certain column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM