将Case Sensitivity添加到CountIf函数

时间:2018-06-15 13:00:41

标签: excel vba excel-vba

目前,我有一些VBA代码没有区分大写/小写值。当我一直在浏览网站时,我看到很多关于删除区分大小写的内容,但不是关于添加它。我的理解是默认情况下Excel VBA区分大小写,但在我的情况下这似乎是错误的,或者我设法在不知情的情况下将其删除。

这是一个例子:在E列中有诸如E01,e01,E02,e02等的值。我需要用不同的大写E和小写E来处理这些值。所以在我的下面的代码中,我说a = "E01"我的意思是E01,而不是e01。但是,当我运行VBA时,两者都被包括在内。

Application.Calculation = xlCalculationManual
Application.DisplayStatusBar = False
Application.ScreenUpdating = False

Dim FirstAddress As String, _
    cF As Range, _
    RowsToCopy As String
Dim a As String

a = "E01"

With ActiveSheet.Columns(5)
    If WorksheetFunction.CountIf(.Cells, a) > 1 Then
        Set cF = .Find(What:=a, _
                    LookIn:=xlFormulas, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByColumns, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False, _
                    SearchFormat:=False)
        If Not cF Is Nothing Then
            FirstAddress = cF.Address
            Do
                cF.EntireRow.Copy
                Sheets("Misc").Range("A" & Sheets("Misc").Range("A" & Rows.Count).End(xlUp).row + 1).PasteSpecial xlPasteValues
                Set cF = .FindNext(cF)
            Loop While Not cF Is Nothing And cF.Address <> FirstAddress
        End If

我是否在这里采取措施以某种方式关闭案例敏感度?关于如何使这个代码示例不包含e01的任何想法?我应该在这里添加像MatchCase:=True这样的东西吗?

感谢您提前提供任何帮助。

2 个答案:

答案 0 :(得分:1)

如果您确实希望将来使用区分大小写的标识

=SUMPRODUCT(--EXACT("A2",A1:A5))

SUMPRODUCT

上使用EXACT

答案 1 :(得分:0)

自从我问这个问题已经有一段时间了,我忘了发布我实施的解决方法,以防万一将来有人遇到这个问题。

我避开了.find,而是使用了排序,然后使用循环来检查某些条件。

排序:

.Sort.SortFields.Add Key:=theRange.Columns(5).Cells, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=theRange.Columns(3).Cells, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        '***** Decide the format of the sort and apply the sorting *****
        With .Sort
            .SetRange theRange
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With

循环:

lr2 = Cells(Rows.Count, 5).End(xlUp).Row
'***** Setting our range for the next loop *****
Set rng2 = Range("E2:E" & lr2)

For Each cell2 In rng2
    '***** IF statement for if the type ID is the same as the next row's type ID *****
    If cell2 = cell2.Offset(1) Then
        '***** Type ID is the same, so we check if it is also a R-D transaction *****
        If cell2.Value Like "*R-D*" Then
            cell2.Value = "R-D"
            '***** If this is a R-D transaction then we do not add the totals of the two lines. Column A total equals Column C total *****
            Total = Total + cell2.Offset(0, -2)
            '***** Placing the summed total into column F *****
            cell2.Offset(0, 1) = Total
            Total = 0
        '***** If the Type ID is the same and the date is different, then we do NOT add the totals together *****
        ElseIf cell2.Offset(0, -3) <> cell2.Offset(1, -3) Then
            Total = Total + cell2.Offset(0, -2)
            '***** Placing the summed total into column F *****
            cell2.Offset(0, 1) = Total
            Total = 0
        ElseIf cell2.Value Like "*ARC*" Then
            cell2.Value = "ARC"
            Total = Total + cell2.Offset(0, -2)
            cell2.Offset(0, 1) = Total
            Total = 0
        Else
        '***** If this is not a R-D or ARC then we add the two totals together and move on to the next row until we find a new Type ID *****
        Total = Total + cell2.Offset(0, -2)
        End If
    '***** If the type ID is not the same as the ID in the next row, then we just carry the trans amt over to Column A from Column C *****
    Else
        Total = Total + cell2.Offset(0, -2)
        '***** Placing the summed total into column F *****
        cell2.Offset(0, 1) = Total
        '***** Resetting the total variable to 0 so we do not add the next row's total to an existing total *****
        Total = 0
    End If
'***** Move on to the next cell and repeat the process *****
Next cell2