Sumif一个单元格包含

时间:2015-08-04 10:23:12

标签: string vba excel-vba sum excel

我的问题是sumif部分,它无法搜索包含字符串'model'的所有单元格,并将有关'model'的值相加。

当我使用sumif函数时,请帮助检查是否有任何错误。

Sub Test()
    Dim file As String
    Dim file1 As String
    Dim wb As Workbook
    Dim wb1 As Workbook
    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim ws2 As Worksheet
    Dim i As Variant
    Dim y As Variant
    Dim LastRow As Long
    Dim LastRow2 As Long
    Dim Model As String
    Dim NextMonth As String
    Dim SumUp As Double

    For i = 3 To LastRow
        For y = 4 To LastRow2

            If InStr(1, ws2.Cells(i, 3).Value, "GOOD FORTUNE CO LTD") And InStr(1, ws2.Cells(i, 10).Value, "FORTUNE ASIA") And InStr(10, ws2.Cells(i, 7).Value, "K") <> 10 Then       'Change Location
                Model = Right(Left(ws2.Cells(i, 7), 9), 6)

                If InStr(1, ws1.Cells(y, 5), Model) And ws1.Cells(y, 21) <> 0 Then   'Change Column Number of 2nd Condition
                    SumUp = Application.WorksheetFunction.SumIf(ws1.Range(ws1.Cells(4, 5), ws1.Cells(y, 5)), InStr(1, ws1.Cells(y, 5), Model), ws1.Range(ws1.Cells(4, 21), ws1.Cells(y, 21)))
                    ws2.Cells(i, 18).Value = SumUp        'Change Column Number of right hand side
                End If
            End If
        Next y
    Next i

    MsgBox "Completed!"
End Sub

2 个答案:

答案 0 :(得分:0)

如果要检查包含文本的单元格,则需要使用通配符:

onBindViewHolder()

答案 1 :(得分:0)

很难确定没有原始数据和所需输出的示例,但似乎您的criteria参数不正确。

正如所写:

InStr(1, ws1.Cells(y, 5), Model)

这将返回一个数字;不是一个字符串。

此外,如果您希望标准参数是包含 Model中的值的单元格,那么您还需要通配符。

也许是这样的:

"*" & Model & "*"