【发布时间】: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声明的目的是什么?我没有看到你在那里的任何地方使用ws。Lastrow是什么?你的拼图有太多缺失的部分。 -
如果您的第二个块正在工作,如果您需要跟踪您有多少个大写值,为什么不在循环内添加一个计数器?