vba - 每当值重复时,将 1 天添加到今天的日期
问题描述
我有一个问题希望你能帮助我。
如果下面的代码在 A 列中找到重复值,则在 B 列中将 1 天添加到今天。但是,如果再次重复,我希望它添加 2 天,依此类推。
我试图在附图中说明代码是如何工作的。所以我想要的是图片中的单元格 B10 是 20/03/2021。我需要让它自动运行,这样它就可以运行任意数量的重复值。
Sub Add_date2()
Dim lastRow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastRow = Range("A65000").End(xlUp).Row
For iCntr = 2 To lastRow
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
If iCntr <> matchFoundIndex Then
Cells(iCntr, 2) = Date + 1
Else
Cells(iCntr, 2) = Date
End If
End If
Next
End Sub
解决方案
使用 Application.Countifs:
Sub Add_date2()
Dim ws As Worksheet
Set ws = ActiveSheet 'better to set the actual sheet WorkSheets("Sheet1")
With ws
Dim lastRow As Long
lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
Dim iCntr As Long
For iCntr = 2 To lastRow
If .Cells(iCntr, 1) <> "" Then
.Cells(iCntr, 2) = Date + Application.CountIfs(.Range(.Cells(2, 1), .Cells(iCntr, 1)), .Cells(iCntr, 1)) - 1
End If
Next
End With
End Sub
推荐阅读
- php - 为已通过 WooCommerce 管理员订单页面添加的元数据添加自定义配置文件字段
- angular - 使用 NPM 镜像在离线环境中更新
- javascript - 无法在 Angular 应用中从 Firebase 控制台接收通知?
- css - 如何解决在Angular中输入一个字符后文本输入失去焦点的问题
- github - Github 浏览器 Codespace 没有加载我的 repo 文件
- python - Python项目依赖管理
- jsx - 我可以在我的网站上全局拦截 ES6 模块导入,例如进行 JSX 编译吗?
- jmeter-5.0 - 运行 Jmeter 脚本时获取 DNSName 组件必须包含字母、数字和连字符错误
- c++ - 是否有 ac api POSIX 可以计算磁盘上的可用空间
- php - 从 WooCommerce 产品循环中删除“第一个”和“最后一个”CSS 类