【问题标题】:VBA Countif UppercaseVBA Countif 大写
【发布时间】:2020-12-09 23:08:15
【问题描述】:

我正在尝试计算包含用户定义范围内所有大写字符的单元格实例的数量,我已经有一些代码可以循环并正确突出显示这些大写单元格,但我正在努力申请该逻辑适用于 VBA 的 Countif 函数。这是我得到的代码,但它给出了不匹配错误:

'count instances of all caps
Dim allcaps As Long
allcaps = Application.CountIf(Range(rngCompany.Cells(1, 1), rngCompany.Cells(Lastrow, 1)), UCase(Range(rngCompany.Cells(1, 1), rngCompany.Cells(Lastrow, 1))))


MsgBox "There are " & allcaps & " uppercase company names to review."

正确高亮单元格的代码是:

'Highlight all caps company names for review
With ws
    For i = 2 To Lastrow
        ' checks if cells in company name col are uppercase
        If rngCompany.EntireColumn.Cells(i, 1).Value = UCase(rngCompany.EntireColumn.Cells(i, 1).Value) Then
            wbk1.Sheets(1).Rows(i).Interior.ColorIndex = 6 '6: Yellow
        Else
            End If
        
    Next i
End With

有没有办法让 countif 代码在循环中以类似的方式工作?谢谢。

【问题讨论】:

  • 请包括您的代码中缺少的部分,例如设置 rngCompany 的位置 - 您的一些 sn-ps 没有意义。 rngCompany.EntireColumn.Cells(i,1).Value - 你为什么在这里包含EntireColumn?您在第二个 sn-p 中的 With 声明的目的是什么?我没有看到你在那里的任何地方使用wsLastrow 是什么?你的拼图有太多缺失的部分。
  • 如果您的第二个块正在工作,如果您需要跟踪您有多少个大写值,为什么不在循环内添加一个计数器?

标签: excel vba loops countif


【解决方案1】:

你可以这样做:

Function AllCapsCount(Target As Range) As Long
    With Target.Parent
        AllCapsCount = .Evaluate("=SUMPRODUCT(--EXACT(" & Target.Address & ",UPPER(" & Target.Address & ")))")
    End With
End Function

【讨论】:

    【解决方案2】:

    Tim 建议在循环中简单地添加一个计数器对我来说是最简单的解决方案,经过漫长的一天后我忽略了这条路!

    以后遇到此问题的任何人的代码示例:

    AllCapsCount = 0
    With ws
        For i = 2 To Lastrow
            ' checks if cells in company name col are uppercase
            If rngCompany.EntireColumn.Cells(i, 1).Value = UCase(rngCompany.EntireColumn.Cells(i, 1).Value) Then
                wbk1.Sheets(1).Rows(i).Interior.ColorIndex = 6 '6: Yellow
            AllCapsCount = AllCapsCount + 1
            Else
                End If
            
        Next i
    End With
    

    【讨论】:

      【解决方案3】:

      如果 UCase 但没有 LCase,则突出显示和计数单元格

      Sub TESTgetAllCapsRange()
          Dim rngCompany As Range
          Set rngCompany = Range("A2:E11")
          rngCompany.Interior.Color = xlNone
          Dim rng As Range: Set rng = getAllCapsRange(rngCompany)
          If Not rng Is Nothing Then
              rng.Interior.Color = vbYellow
              Dim AllCaps As Long: AllCaps = rng.Cells.CountLarge
              If AllCaps > 1 Then
                  MsgBox "There are " & AllCaps _
                      & " uppercase company names to review."
              Else
                  MsgBox "There is 1 uppercase company name to review."
              End If
          Else
              MsgBox "There are no uppercase company names to review."
          End If
      End Sub
      
      Function getAllCapsRange(rng As Range) As Range
          If Not rng Is Nothing Then
              Dim tRng As Range
              Dim aRng As Range
              Dim cel As Range
              For Each aRng In rng.Areas
                  For Each cel In aRng.Cells
                      If Not IsError(cel) Then
                          If containsUCaseButNoLCase(cel.Value) Then
                              buildRange tRng, cel
                          End If
                      End If
                  Next cel
              Next aRng
              If Not tRng Is Nothing Then
                  Set getAllCapsRange = tRng
              End If
          End If
      End Function
      
      Function containsUCaseButNoLCase(ByVal CheckString As String) As Boolean
          ' Check if there is an upper case character.
          If StrComp(CheckString, LCase(CheckString), vbBinaryCompare) <> 0 Then
              ' Check if there are no lower case characters.
              If StrComp(CheckString, UCase(CheckString), vbBinaryCompare) = 0 Then
                  containsUCaseButNoLCase = True
              End If
          End If
      End Function
      
      Sub buildRange(ByRef BuiltRange As Range, AddRange As Range)
          If Not AddRange Is Nothing Then
              If Not BuiltRange Is Nothing Then
                  Set BuiltRange = Union(BuiltRange, AddRange)
              Else
                  Set BuiltRange = AddRange
              End If
          End If
      End Sub
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-08-25
        • 1970-01-01
        • 1970-01-01
        • 2017-06-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多