Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 将区分大小写添加到CountIf函数_Vba_Excel - Fatal编程技术网

Vba 将区分大小写添加到CountIf函数

Vba 将区分大小写添加到CountIf函数,vba,excel,Vba,Excel,目前,我有一些VBA代码不区分大小写值。当我环顾整个网站时,我看到了很多关于删除区分大小写的内容,但没有看到添加区分大小写的内容。我的理解是Excel VBA默认情况下是区分大小写的,但在我的情况下这似乎是错误的,或者我在不知不觉中成功地删除了它 下面是一个示例:在E列中有一些值,如E01、E01、E02、E02等。我需要以不同的方式对待大写和小写的值。因此,在我下面的代码中,当我声明a=“E01”时,我真正的意思是E01,而不是E01。但是,当我运行VBA时,这两个都包括在内 Applicat

目前,我有一些VBA代码不区分大小写值。当我环顾整个网站时,我看到了很多关于删除区分大小写的内容,但没有看到添加区分大小写的内容。我的理解是Excel VBA默认情况下是区分大小写的,但在我的情况下这似乎是错误的,或者我在不知不觉中成功地删除了它

下面是一个示例:在E列中有一些值,如E01、E01、E02、E02等。我需要以不同的方式对待大写和小写的值。因此,在我下面的代码中,当我声明
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
Application.Calculation=xlCalculationManual
Application.DisplayStatusBar=False
Application.ScreenUpdating=False
将FirstAddress设置为字符串_
cF作为范围_
行复制为字符串
像线一样变暗
a=“E01”
使用ActiveSheet.Columns(5)
如果工作表function.CountIf(.Cells,a)>1,则
设置cF=.Find(What:=a_
LookIn:=xl公式_
看:=xlother_
SearchOrder:=xlByColumns_
SearchDirection:=xlNext_
MatchCase:=假_
SearchFormat:=False)
如果不是,那么cF什么都不是
FirstAddress=cF.地址
做
cF.EntireRow.Copy
工作表(“杂项”)范围(“A”和工作表(“杂项”)范围(“A”和行数)。结束(xlUp)。行+1)。粘贴特殊xlPasteValues
设置cF=.FindNext(cF)
循环而非cF为Nothing且cF.Address为FirstAddress
如果结束
我是不是做了些什么来关闭区分大小写的功能?关于如何使此代码示例不包含e01,有什么想法吗?我应该在这里的某个地方添加类似于
MatchCase:=True
的内容吗


感谢您提前提供的帮助。

如果您将来需要区分大小写的countif

=SUMPRODUCT(--EXACT(“A2”,A1:A5))


使用
SUMPRODUCT
on
EXACT

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

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

分类:

循环:

lr2=单元格(Rows.Count,5).End(xlUp).Row
“******设置下一个循环的范围*****
设置rng2=范围(“E2:E”和lr2)
对于rng2中的每个cell2
类型ID与下一行的类型ID相同时的'****IF语句*****
如果cell2=cell2.偏移量(1),则
'****类型ID相同,因此我们检查它是否也是R-D事务*****
如果单元格2.的值像“*R-D*”那么
单元格2.Value=“R-D”
“******如果这是一笔研发交易,那么我们不会将这两行的总数相加。A列总计等于C列总计*****
总计=总计+单元2。偏移量(0,-2)
“******将总计放入F列*****
单元2.偏移量(0,1)=总计
总数=0
“******如果类型ID相同而日期不同,则不将总数相加*****
ElseIf cell2.偏移量(0,-3)cell2.偏移量(1,-3)然后
总计=总计+单元2。偏移量(0,-2)
“******将总计放入F列*****
单元2.偏移量(0,1)=总计
总数=0
ElseIf cell2.类似“*弧*”的值,然后
单元格2.Value=“弧”
总计=总计+单元2。偏移量(0,-2)
单元2.偏移量(0,1)=总计
总数=0
其他的
'****如果这不是一个R-D或ARC,那么我们将这两个总数相加,并继续下一行,直到找到一个新的类型ID*****
总计=总计+单元2。偏移量(0,-2)
如果结束
“******如果类型ID与下一行中的ID不同,那么我们只需将trans amt从C列带到A列*****
其他的
总计=总计+单元2。偏移量(0,-2)
“******将总计放入F列*****
单元2.偏移量(0,1)=总计
'****将total变量重置为0,这样我们就不会将下一行的total添加到现有的total中*****
总数=0
如果结束
“******转到下一个单元格并重复此过程*****
下一单元2

您回答了自己的问题-是将
MatchCase:=False
更改为
MatchCase:=True
。更多详情请访问。但是你真的不需要
If WorksheetFunction.CountIf…
。它在CountIf上,如果不是find部分,尽管@BigBen我同意,CountIf是不需要的,如果count是0,你的find将是
@Nathan_Sav是的,很好地说,如果区分大小写的计数是0,你的find将是
@BigBen谢谢你的回复。我想我在这里的目的实际上需要CountIf。我没有费心详细讨论它,因为它与这个问题无关,但我所做的只是采取行动,如果有2个或更多的“E01”,如果只有1个“E01”,那么我想跳过它。我想是伯爵干的。因为我可以有任何数量的“E01”取决于一天。
.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