[英]VBA to drag a row with formula but for current month only
I have a sheet that shows monthly data. 我有一张显示每月数据的工作表。 It includes columns for the date and a countif formula. 它包括日期和countif公式的列。
What I want to do: When the code runs on every first day of the month, VBA code will automatically hide the previous month (eg July) and creates new rows for current month (eg August). 我想做什么:当代码在该月的每个第一天运行时,VBA代码将自动隐藏上个月(例如7月)并为当月(例如8月)创建新行。 These rows for current month should also have the dynamic countif formula. 当月的这些行还应具有动态countif公式。
What I'm trying to do: 我正在尝试做的是:
Set laslastday = Range("K1").Value ' Last day of previous month
Set sht = ActiveSheet.UsedRange.Columns("A").SpecialCells(xlCellTypeVisible)
LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
For Each cl In sht.Cells
With cl
If (.Value = laslastday) Then
.Offset(1, 0).Activate
'-- dont know what's next
End If
End With
Next
Here's a sample image... 这是示例图片...
It should start the copying of rows on the Yellow cell but only until August 31. Then hide the July rows. 它应该开始复制Yellow单元格上的行,但只复制到8月31日。然后隐藏7月的行。
It should also do the same as to when this will be generated. 它也应该与何时生成它相同。 (eg September, October, etc.) (例如,9月,10月等)
Notes: 笔记:
k
将从k
列的LastRow中选择最后一天 3:LastRow
将隐藏3:LastRow
行 Code: 码:
With ActiveSheet
LastRow = .Cells(ActiveSheet.Rows.Count, "B").End(xlUp).row
lastday = Range("K" & LastRow).Value
dd = Day(DateSerial(Year(lastday + 1), Month(lastday + 1) + 1, 0))
For i = 1 To dd
.Cells(LastRow + i, "K").Value = lastday + i
.Cells(LastRow + i, "K").NumberFormat = "mm/dd/yyyy"
Next
.Range("L" & LastRow & ":N" & LastRow).Copy
.Range("L" & LastRow + 1 & ":N" & LastRow + dd).PasteSpecial xlPasteFormulas
.Rows(3 & ":" & LastRow).EntireRow.Hidden = True
End With
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.