excel - 如何在 VBA 中使用 COUNTIF?
问题描述
我正在尝试使用 VBA 来计算一个范围内的日期数。我的范围扩大了列和行,我试图找到最后一行和最后一列,但似乎无法获得我想要的结果。
如何使用COUNTIF
函数中的最后一行和最后一列?
这是我到目前为止的代码:
Sub count_Month()
Dim ws As Worksheet
Dim LastCol As Long
Dim count As Long
Dim lastrow As Long
lastrow = Sheet3.Range("M" & Rows.count).End(xlUp).Row
Set ws = Sheets("clientmenu")
If Application.WorksheetFunction.CountA(ws.Cells) = 0 Then
LastCol = 1
Else
LastCol = ws.Cells.Find(What:="*", _
After:=ws.Range("A8"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
End If
With Application.WorksheetFunction
count = .CountIfs(Sheet2.Range("M8" & LastCol), ">=" & Sheet2.Range("R25"), Sheet2.Range("M8" & LastCol), "<=" & Sheet2.Range("R26"))
End With
Debug.Print count
End Sub
解决方案
Sub count_Month()
Dim ws As Worksheet
Dim LastCol As Long, LastRow As Long, count as Long, rng as Range
LastRow = Sheet3.Range("M" & Sheet3.Rows.count).End(xlUp).Row
Set ws = Sheets("clientmenu")
If Application.WorksheetFunction.CountA(ws.Cells) = 0 Then
LastCol = 1
Else
LastCol = ws.Cells.Find(What:="*", _
After:=ws.Range("A8"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
End If
With Sheet2
Set rng = .Range(.Cells(8, 13), .Cells(LastRow, LastCol))
End With
count = Application.WorksheetFunction.CountIfs(rng, ">=" & Sheet2.Range("R25"), _
rng, "<=" & Sheet2.Range("R26"))
Debug.Print count
End Sub
推荐阅读
- node.js - 如何将当前 url 分配给变量?NodeJS Selenium-webdriver
- kotlin - 如何锁定两个协程但允许原始协程进入
- mysql - 如何在MYSQL存储过程中的字符串动态列表中搜索字符串
- java - 检查 2 个不区分大小写的字符串或使用 equalsIgnoreCase 方法
- datepicker - AntD Range Picker如何只启用3个月?
- excel - 使用 VBA 代码自动填充单元格,其中包含来自不同列的条件
- .net - 实体框架问题 - 启用迁移
- windows - 在 git bash 中运行 sh 文件甚至会导致终端中显示变量赋值语句
- ios - 警告:无法为签名者“iPhone Distribution:XXXXXX”构建自签名根的链
- java - 如何使用在 AndroidManifest 上标记的关于声明的shakebugs ScreenRecordingService 防止此android 构建消息?