简体   繁体   English

SUM公式VBA代码

[英]SUM formula vba code

How is it possible to sum two columns in an excel file into another column. 如何将Excel文件中的两列求和成另一列。 I want to sum Column A with Column B and the result should be shown in column C, but how can I do it in vba? 我想将A列与B列相加,结果应显示在C列中,但是如何在vba中执行呢?

For example: 例如:

Col A. 可乐。

  1. 1 1个
  2. 2 2
  3. 3 3

Col B. 上校B.

  1. 1 1个
  2. 2 2
  3. List item 项目清单

Col C. 上校C.

  1. 2 2
  2. 4 4
  3. 6 6

if that's all you need, why not just use the excel formula for cell C1 "=SUM(A1,B1)"? 如果只需要这些,为什么不对单元格C1“ = SUM(A1,B1)”只使用excel公式? if it's part of a more complex vba macro you can use 如果它是更复杂的vba宏的一部分,则可以使用

cells(1,3).Formula = "=SUM(A1,B1)"
Range("C1:C3").FillDown

There many ways. 有很多方法。 For example: 例如:

Sub AddColumns()
    Dim i As Long
    For i = 1 To 3
        Cells(i, 3).Value = Cells(i, 1).Value + Cells(i, 2).Value
    Next i
End Sub

You could also use Evaluate() which is slightly faster and slightly more obscure. 您也可以使用Evaluate() ,它速度稍快,但晦涩难懂。

You're not posing the problem very well ... if you're interested in adding values in a column (like you) wrote, I don't see any reason why you would want to use VBA at all. 您并不是很好地解决了这个问题……如果您有兴趣在所写的列中添加值(如您所写),那么我根本看不到您为什么要使用VBA的任何原因。 Just add the values in the cells directly. 只需直接在单元格中添加值即可。

Apart from that, you can always use a function like 除此之外,您始终可以使用类似

Function DoColumnStuff( column_1 as Range, column_2 as Range ) as Variant

  Dim returnColumn() as Variant

  ' resize returnColumn to a sensible size and
  ' populate it with sensible values, derived out of
  ' column_1 and column_2

  DoColumnStuff = returnColumn
End Function

So, you could do with your arguments whatever your heart desires and return that as a column. 因此,您可以随心所欲地处理论点,并将其作为专栏返回。 Two things to note: Firstly, if you want the function to return a column , you will have to resize returnColumn to a 2-dimensional array with 1 column dimension (eg 10 rows, 1 column ~~> use Redim returnColumn( 1 to 10, 1 to 1 ) for resizing). 需要注意两件事:首先,如果您希望函数返回一 ,则必须将returnColumn大小调整为具有一returnColumn的二维数组(例如10行,1列~~>使用Redim returnColumn( 1 to 10, 1 to 1 )以调整大小)。 Secondly, you need to insert the function as a Matrix-Function in Excel, ie select the whole output range, type in the formula and press Shift-Enter 其次,您需要在Excel中将函数插入为矩阵函数,即选择整个输出范围,键入公式并按Shift-Enter

As Gary's Student said, there are many ways, one is below if 正如Gary的学生所说,有很多方法,如果

A1 contains 1 and B1 contains 1 A2 contains 2 and B2 contains 2 A3 contains 3 and B3 contains 3 A1包含1和B1包含1 A2包含2和B2包含2 A3包含3和B3包含3

in C1 Column paste the below formula 在C1列中粘贴以下公式

 =IF(OR(ISBLANK(A1),ISBLANK(B1)),"Either A or B is Blank",SUM(A1:B1))

and select the column C1 to C3 and press Ctrl + D 然后选择列C1至C3,然后按Ctrl + D

in VBA, 在VBA中

Sub Sample()
lastrow = ActiveSheet.UsedRange.Rows.Count
For i = 1 To lastrow
If Cells(i, 1) = "" And Cells(i, 2) = "" Then
Cells(i, 3) = "Both are Blank"
ElseIf Cells(i, 1) <> "" And Cells(i, 2) = "" Then
Cells(i, 3) = "B is Blank"
ElseIf Cells(i, 1) = "" And Cells(i, 2) <> "" Then
Cells(i, 3) = "A is Blank"
ElseIf Cells(i, 1) <> "" And Cells(i, 2) <> "" Then
Cells(i, 3) = Cells(i, 1) + Cells(i, 2)
End If
Next i
End Sub

or you can use Function. 或者您可以使用功能。 Directly into excel sheet like formula Simply type sumformula(A1,B1) in C1, results will appears in C1 row after enter. 直接插入excel表格(如公式)中。只需在C1中键入sumformula(A1,B1),输入后结果将显示在C1行中。

please see below 请看下面

Function sumformula(a As String, b As String)
If a = "" And b = "" Then
    sumformula = "Both are Blank"
ElseIf a <> "" And b = "" Then
    sumformula = "B is Blank"
ElseIf a = "" And b <> "" Then
    sumformula = "A is Blank"
ElseIf a <> "" And b <> "" And IsNumeric(a) = False And IsNumeric(b) = True Then
    sumformula = "Column A is not a number"
ElseIf a <> "" And b <> "" And IsNumeric(a) = True And IsNumeric(b) = False Then
    sumformula = "Column B is not a number"
ElseIf a <> "" And b <> "" And IsNumeric(a) = True And IsNumeric(b) = True Then
    sumformula = WorksheetFunction.Sum(a, b)
End If
End Function

Hope it helps 希望能帮助到你

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM