简体   繁体   English

如果A列包含“ D列”且B列包含“ E列”,则添加值

[英]If column A contains “column D” AND column B contains “column E” THEN add value

I'd like to write a macro that reads words in Column A and B, to see if they match words in Column E and F respectively, then adds the value in Column C to Column G. 我想编写一个宏来读取A列 B列中的单词,以查看它们是否分别与E列 F列中的单词匹配, 然后 C列中的值添加到G列中。

For example: 例如:

在此处输入图片说明

You can see, for example, that there are two instances of "Lion" and "Horse" in Columns A and B, so Column G has the total of the two (10 + 8 = 18). 例如,您可以看到A列和B列中有“ Lion”和“ Horse”两个实例,因此G列具有这两个实例的总数(10 + 8 = 18)。

Unfortunately, the attempt I made just copies the values from Column C to Column G: 不幸的是,我所做的尝试只是将值从C列复制到G列:

Sub CombineAnimals()

lastRow = Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

For x = 1 To lastRow
    If InStr(1, Sheets("Sheet1").Cells(x, 1), Cells(x, 5)) <> 0 _
    And InStr(1, Sheets("Sheet1").Cells(x, 2), Cells(x, 6)) <> 0 Then
        Sheets("Sheet1").Cells(x, 7).Value = _
        Sheets("Sheet1").Cells(x, 7).Value + Cells(x, 3)
    End If
Next x

End Sub

I know I'm doing something wrong with the "x" (and probably many other things), but I can't figure out a way to make it work. 我知道我对“ x”(可能还有很多其他事情)做错了事,但我想不出一种使它起作用的方法。 Is there any way to change this so it adds the totals together as it does in the my example picture? 有什么办法可以改变它,使其像我的示例图片中那样将总数相加?

Many thanks for your help. 非常感谢您的帮助。

Does it have to be a macro? 它必须是宏吗? you could just put in =SUMIFS(C:C,A:A,E2,B:B,F2) into G2 and fill down: 您可以将=SUMIFS(C:C,A:A,E2,B:B,F2)放入G2并填写:

在此处输入图片说明

If you really want to have this in a macro, it'd be something like this: 如果您真的想在宏中使用它,则可能是这样的:

Sub CombineAnimals()

    Range("G2").FormulaR1C1 = "=SUMIFS(C[-4],C[-6],RC[-2],C[-5],RC[-1])"
    Range("G2").AutoFill Destination:=Range("G2:G" & Range("F" & ActiveSheet.Cells.Rows.Count).End(xlUp).Row)

End Sub

Unless there is good reason not to, a simple SUMIFS loop should work: 除非有充分的理由,否则一个简单的SUMIFS循环应该起作用:

Sub CombineAnimals()
With Sheets("Sheet1")
    lastRow = .Range("E" & .Rows.Count).End(xlUp).Row

    For x = 2 To lastRow
        .Cells(x, "G").Value = Application.SumIfs(.Range("C:C"), .Range("A:A"), .Cells(x, "E"), .Range("B:B"), .Cells(x, "F"))
    Next x
End With
End Sub

Something like this will work, although it may be a bit overkill: 这样的事情可能会起作用,尽管可能有些过大:

You concatenate the first two cells in a string and this string is used as a key for a dictionary. 您将字符串中的前两个单元连接起来,并且此字符串用作字典的键。 Then, whenver something similar is found, you add its value to the dictionary. 然后,只要找到相似的内容,便将其值添加到字典中。 At the end you may print the dictionary. 最后,您可以打印字典。

Option Explicit

Public Sub TestMe()

    Dim dict            As Object
    Dim rngCell         As Range

    Dim rngInput        As Range
    Dim strInput        As String
    Dim dblInput        As Double
    Dim lngCounter      As Long
    Dim varKey          As Variant

    Set dict = CreateObject("Scripting.Dictionary")
    Set rngInput = ActiveSheet.Range("A2:A6")


    For Each rngCell In rngInput
        strInput = rngCell.Value & rngCell.Offset(0, 1).Value
        dblInput = rngCell.Offset(0, 2).Value

        If dict.exists(strInput) Then
            dict(strInput) = dict(strInput) + dblInput
        Else
            dict.Add strInput, dblInput
        End If
    Next rngCell


    For Each varKey In dict.keys
        Debug.Print varKey, dict(varKey)
    Next varKey


End Sub

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

相关问题 如果A列包含x AND B列包含y然后添加值 - If column A contains x AND column B contains y THEN add value 如果B列的单元格值包含A单元格值 - If column B cell value contains A cell value 查找A列中的值是否包含B列的值? - Find if value in column A contains value from column B? 如果列B在Excel中包含正确的值,则突出显示列A中的单元格 - Highlighting cells in column A if column B contains the correct value in Excel Countifs列A包含“字符串”,列B以“ 0”开头 - Countifs column A contains “String” & column B starts with “0” 如果 A 列包含搜索词,如何添加/替换 B 列中的值 - How do I add/replace a value in column B if column A contains the searched word 将A列和E列的excel值与B列和D列中的名称匹配 - Matching excel value of column A and E to a name in column B and D 如果A列包含文本,则将值从B列中的单元格复制到B列中的相应项目 - If column A contains text, copy value from cell in Column B to corresponding item in Column B 如果 X 列包含值,则将 Y 列值添加到工作表范围 - If column X contains value, add column Y value to worksheet range Excel VBA-如果列B包含任何值,则使用值更新列A。 如果列B不包含任何值,则不要运行宏 - Excel VBA - Update column A with a value if column B contains any value. If column B contains no values then do not run the macro
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM