vba - If Monday then add x y z
问题描述
Can you please advise how to restructure my code. At the moment I am adding a date -1 to the bottom of the column D (every day). However, when it's Monday I need to add the date -3, then date -2 and then date -1 to the bottom of the column D.
Here is my current code:
y.Activate
With y1
Dim d As Long
d = Cells(Rows.Count, "D").End(xlUp).Row + 1 'there was an error in the code, I have changed the N to d
Cells(d, "D").Value = Date - 1
End With
My assumption would be the IIf statement? But then again I guess it requires a loop as well.
Thanks
解决方案
Right before your Cells(d, "D").Value = Date - 1
line, throw in a:
If Weekday(Now()) = 2 Then
Cells(d, "D").Value = Date - 3
Cells(d + 1, "D").Value = Date - 2
Cells(d + 2, "D").Value = Date - 1
Else
Cells(d, "D").Value = Date - 1
End If
The If Weekday(Now())
will compare the day of the week (1 is Sunday by default I believe). So by checking to see if it's "2" then you are able to run what you'd like. I'm not sure if this is exactly what you were aiming for, but this will place three different date values, starting from Friday and ending at Sunday.
Probably not the best solution, but this was my understanding of the question. If I'm off let me know and I'll adjust my answer.
推荐阅读
- html - 为什么 html select 用溢出滚动覆盖容器?
- javascript - 如何在 Node 中承诺和等待 setImmediate?
- javascript - 如何从反应中的儿童道具访问儿童的属性
- python - 打印嵌入 discord.py
- phpmyadmin - 如何在 phpmyadmin 中自动美化设计器
- asp.net-mvc - 如何使用实体框架打印存储在 ASP.NET MVC 中的 Viewbag 中的连接查询数据?
- postgresql - where 子句中 jsonb_path_match 和 ->> 之间的性能/逻辑差异是什么
- youtube-api - 哪个youtuve v3 api不支持etag
- python - 第 (39) 行 IndexError:弹出索引超出范围
- java - 使用java在spark中的函数之间