将值从一个单元格添加到另一个单元格,然后重置单元格值.电子表格 [英] Add value from one cell to another then reset cell value. Excel

查看:75
本文介绍了将值从一个单元格添加到另一个单元格,然后重置单元格值.电子表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望有人可以回答我们.

Hope someone can answer us.

我们要执行以下操作:

在A1中输入一个值时,该值将添加到B1的值中.

When a value is entered in A1, this value is added to the value of B1.

然后我们希望A1的值也重置为0,但保持B1的值

We then want the value of A1 too reset to 0, but keep the value of B1

这在excel中可行吗?

Is this possible in excel?

推荐答案

在工作表的VBA专用模块上(右键单击报告"选项卡,然后单击查看代码"),输入以下代码:

On the worksheet's VBA private module (right-click the report tab and hit "View Code"), enter the following code:

Private Sub Worksheet_Change(ByVal Target As Range)
    Application.EnableEvents = False
    If Target.Address = Range("a1").Address Then
        Range("b1") = Range("b1") + Range("a1")
        Range("a1").ClearContents
    End If
    Application.EnableEvents = True
End Sub

每当对工作表进行更改时,都会调用

Worksheet_Change .

Worksheet_Change is called whenever a change is made to the worksheet.

Application.EnableEvents = False 阻止代码连续运行(如果没有运行,对B1的更改也将称为Worksheet_change).

Application.EnableEvents=False prevents the code from running continuously (without it, the change to B1 would also call Worksheet_change).

此代码假定B1中的值始终是数字(或空白).如果其中可能包含字符文本,则需要进行检查以重置其值或不执行增量操作.

This code assumes that the value in B1 is always numeric (or blank). If it may contain character text, then a check will need to be put in place to either reset its value or not do the increment.

这篇关于将值从一个单元格添加到另一个单元格,然后重置单元格值.电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆