基于小写(区分大小写)字母的求和单元格?

时间:2018-04-16 17:02:31

标签: excel vba excel-vba

我正在编写一个程序,该程序应该只对具有小写c的行求和。我现在的代码,以c开头的所有行的总和,无论是大写还是小写。我如何编写它以使其区分大小写? 以下是我到目前为止的情况:

Sub summ()
Dim iArea As Long

With Worksheets("K00304.RPT")
    With .Range("A14", .Cells(.Rows.Count, 1).End(xlUp))
        .Cells(2, 1).Value = "ZERO"
        .AutoFilter field:=1, Criteria1:="ZERO*"
        With .Resize(.Rows.Count - 
1).Offset(1).SpecialCells(xlCellTypeVisible) '.Offset(-1)
            For iArea = 1 To .Areas.Count - 1
                With .Parent.Range(.Areas(iArea).Offset(1), .Areas(iArea + 
1).Offset(-1))
                    Worksheets("Total").Cells(Rows.Count, 
"AF").End(xlUp).Offset(1).Value = WorksheetFunction.SumIf(.Cells, "c*", 
.Offset(, 7))
                End With
            Next
        End With
        .Cells(2, 1).ClearContents
    End With
    .AutoFilterMode = False
End With

非常感谢!

2 个答案:

答案 0 :(得分:0)

您可以将字符与StrConv()进行比较:

Sub t()
Dim cel As Range
Set cel = Range("A1")
If StrConv(Left(cel, 1), vbProperCase) = Left(cel, 1) And Left(cel, 1) = "C" Then
    'There's a match, the left letter is capitalized.
    Debug.Print "'C' is present"
End If
End Sub

编辑:实际上,只检查If LEFT(cel,1) = "C" Then似乎也有效。

答案 1 :(得分:0)

我会使用Evaluate函数和sumproduct公式,希望这可行。

Sub summ()
Dim iArea As Long

With Worksheets("K00304.RPT")
    With .Range("A14", .Cells(.Rows.Count, 1).End(xlUp))
        .Cells(2, 1).Value = "ZERO"
        .AutoFilter field:=1, Criteria1:="ZERO*"
        With .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible) '.Offset(-1)
            For iArea = 1 To .Areas.Count - 1
                With .Parent.Range(.Areas(iArea).Offset(1), .Areas(iArea + 1).Offset(-1))

                    Dim criteria_range As String
                    Dim sum_range As String
                    Dim criteria As String
                    Dim sum_ifs_case_sensitive As Long

                    criteria = "c"
                    criteria_range = .Cells.Address
                    sum_range = .Offset(, 7).Address
                    sum_ifs_case_sensitive = Evaluate("SUMPRODUCT(--(ISNUMBER(FIND(" & Chr(34) & criteria & Chr(34) & "," & criteria_range & ")))," & sum_range & ")")

                    Worksheets("Total").Cells(Rows.Count, "AF").End(xlUp).Offset(1).Value = sum_ifs_case_sensitive

                End With
            Next
        End With
        .Cells(2, 1).ClearContents
    End With
    .AutoFilterMode = False
End With

End Sub