简体   繁体   English

使用宏将Excel 2016中另一个单元格的选定单元格值求和

[英]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.

相关问题 Excel 2016:IF函数使用单元格相对于另一个单元格的位置 - Excel 2016: IF function using position of cell relative to another cell Excel宏,要基于另一个单元格值复制和粘贴单元格值? - Excel macro, to copy and paste a cell value based on another cell value? 如何使用参考单元格的值对另一个工作表中的excel数据求和 - How to sum excel data in another worksheet, using the value of a reference cell Excel宏基于另一个单元格的通用值将两个单元格的值相加(求和) - Excel macro to add up (sum) the value of two cells based on the common value of another cell 使用宏在Excel单元格中插入数组值 - insert array value in excel cell using macro Excel VBA宏 - 使用公式中的单元格值 - Excel VBA macro - using cell value in formula 根据另一个单元格的单元格颜色更改单元格颜色 excel 2016 - change Cell colour based on cell colour of another cell excel 2016 Excel宏:使用宏将字符串与单元格值连接在一起 - Excel Macro : Concatenate string with cell value using macro Excel 如何使选项卡 2 中的单元格仅在选项卡 1 的下拉单元格中的某个值被选中时才显示选项卡 3 中的单元格的值 - Excel How to make cell in tab 2 show the value of a cell in tab3 only if a certain value in dropdown cell from tab 1 is slected Excel宏修剪单元格值 - Excel Macro to Trim a cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM