[英]Sum slected cell value with another cell in Excel 2016 using macro
I am preparing a spreadsheet that does certain functions automatically. 我正在准备一个自动执行某些功能的电子表格。
I have the following rows: 我有以下几行:
A, B, C, D, E,F, AF, G, Date, Formatted Date A,B,C,D,E,F,AF,G,日期,格式化日期
My purpose is that when I enter values to cells A to F, the sum is automatically displayed in AF and G is the Sub Total. 我的目的是,当我向A到F单元格输入值时,总和会自动显示在AF中,而G是小计。 For example, initially if the values of A to F are 1 each then AF is the sum total of AF and would display 6, G will be 6 itself as the inital value will be 0, so 0+6 = 6, however I want if I change the values of AF to 2 each, G should take the previous value (which is for example 6) and sum it to the present value of AF (sum of AF = 12) and change hte cell value of G to 18 例如,最初,如果A到F的值分别为1,则AF是AF的总和,并且将显示6,G本身将为6,因为初始值将为0,所以0 + 6 = 6,但是我想要如果我将AF的值分别更改为2,则G应采用先前的值(例如6)并将其求和为AF的当前值(AF的总和= 12),并将G的单元格值更改为18
I am trying to create a dynamic macro for this function. 我正在尝试为此功能创建一个动态宏。 I have attained everything else except Replacing G with the sum of present value + the present value of AF. 除了用G的现值+ AF的现值代替G之外,我已经完成了所有其他工作。
Any help would be greatly appreciated. 任何帮助将不胜感激。
This is my current macro VBA code 这是我当前的宏VBA代码
Sub FetchDate()
'
' FetchDate Macro
'
' Keyboard Shortcut: Ctrl+q
'
ActiveCell.FormulaR1C1 = "=TODAY()"
ActiveCell.Offset(0, 1).Range("A1").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[-1],""mmmm d, yyyy"")"
End Sub
Sub Sum()
'
' Sum Macro
'
' Keyboard Shortcut: Ctrl+w
'
ActiveCell.FormulaR1C1 = "=SUM(RC[-6]:RC[-1])"
ActiveCell.Offset(0, 1).Range("A1").Select
End Sub
Rohith, as I understand your question is how to provide the running sum. Rohith,据我所知,您的问题是如何提供流动资金。 Basically you want to add the first sum value to the next sum value. 基本上,您想将第一个和值添加到下一个和值。 Hopefully this code can help you to resolve your question. 希望这段代码可以帮助您解决问题。 Here you just need to store the first sum value to cell or you can store in variable. 在这里,您只需要将第一个和值存储到单元格中,或者可以将其存储在变量中。
Sub SumData()
Range("H1") = Range("G1").Value
If Range("H1") = "" Then
Range("G1") = "=Sum(A1:F1)"
Range("H1") = Range("G1").Value
Range("G1") = Range("G1").Value
ElseIf Range("H1") <> "" Then
Range("G1") = "=Sum(A1:F1)"
Gvalue = Range("G1").Value
Hvalue = Range("H1").Value
sumvalue = Gvalue + Hvalue
Range("G1") = sumvalue
End If
End Sub
Thank you all for all the support. 谢谢大家的支持。
Using the concept mentioned above, I made it work using the following code: 使用上面提到的概念,我使用以下代码使其起作用:
ActiveCell.FormulaR1C1 = "=SUM(RC[-1],RC[-2])"
ActiveCell.Select
Selection.Copy
ActiveCell.Offset(0, -1).Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
ActiveCell.Offset(0, 1).Range("A1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "0"
ActiveCell.Offset(0, 1).Range("A1").Select
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.