如何满足条件时,如何将一个单元格的值添加到另一个单元格 [英] How do I add a value of one cell to another cell when criteria is met

查看:396
本文介绍了如何满足条件时,如何将一个单元格的值添加到另一个单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,基本上我想做的是:

如果我在单元格中输入新值,则自动将此数字添加到另一个单元格的现有数字。因此,当我向B24添加一个新值时,D24的值会自动加上值,如果没有值则不做任何事情。感谢您的快速帮助



我尝试过:



使用Sumif但是我需要选择一个范围,但我只需要一个单元格的值。 Just Sum(B24; D24)因为ciruclation而给我一个错误...

Hey, so basaically what I want to do is:
if I enter a new value into a cell, automaticaly add this number to an existing number of another cell. So when I add a new value into B24, the value of D24 automaticaly gets added up by the value, if no value is there dont do anything. Thanks for the quick help

What I have tried:

using Sumif but I needed to eselect a range, but i only need the value of one cell. Just Sum(B24;D24) gives me an error because of ciruclation...

推荐答案

在VBA编辑器中(Alt-F11到达它)双击您希望这种情况发生的表格。



输入以下代码:
In the VBA editor (Alt-F11 to get to it) double click on the Sheet you want this to happen on.

Enter the following code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    
    'This checks for any entry in column B
    If Application.Intersect(Range("B:B"), Range(Target.Address)) Is Nothing Then
        Exit Sub
    End If
    
    'Or check that it is the specific range you are interested in
    If Target.Address <> "


B


24 然后 退出 Sub

' 如果D24目前没有任何内容,那么无其他事情
如果 IsEmpty(范围( D24))然后 退出 Sub < span class =code-comment>' 注意你不能使用IsBlank

' 如果用户没有输入数字,则无需再做任何事情
如果 IsNumeric(Target.Value)然后 退出 Sub

' 如果D24中的当前值不是数字,则无需再做任何事情
< span class =code-comment>' (绝不相信用户输入您期望的数据!)
如果 IsNumeric(范围( D24)。值)然后 退出 Sub

' 将新值添加到正在运行总计
范围( D24)。值=范围( D24)。Va lue + Target.Value

结束 Sub
24" Then Exit Sub 'If there is nothing currently in D24 then nothing more to do If IsEmpty(Range("D24")) Then Exit Sub 'Note you cannot use IsBlank 'If user did not enter a number then there is nothing more to do If Not IsNumeric(Target.Value) Then Exit Sub 'If the current value in D24 is not numeric then nothing more to do '(Never trust a user to enter the data you are expecting!) If Not IsNumeric(Range("D24").Value) Then Exit Sub 'Add the new value to the running total Range("D24").Value = Range("D24").Value + Target.Value End Sub

希望这些评论足以引导您了解正在发生的事情。只要在特定工作表上发生更改,就会触发Worksheet_Change事件。

Hopefully the comments are enough to guide you on what is going on. The Worksheet_Change event is fired whenever something changes on that particular sheet.


这篇关于如何满足条件时,如何将一个单元格的值添加到另一个单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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