简体   繁体   English

VBA使用公式拖动行,但仅当月

[英]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: 我正在尝试做的是:

  1. I am trying to get the last row address of the previous month(July), then add an offset to start the series for the current month(August). 我试图获取上个月(7月)的最后一行地址,然后添加一个偏移量以开始本月(8月)的系列。
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: 笔记:

  • Will pick the lastday from LastRow of Column k 将从k列的LastRow中选择最后一天
  • Will put the Dates for a Month 将放置一个月的日期
  • Will hide Rows from 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM