【问题标题】:Adding Next Month Programmatically (VBA) to a Dynamic Range Table in Excel以编程方式 (VBA) 将下个月添加到 Excel 中的动态范围表
【发布时间】:2017-02-09 15:52:15
【问题描述】:

我有一个动态范围表 [Defined Table Name = MainTable]。我还有代码可以让我找到 DateRange 中的最后一个单元格(A 列的定义名称范围)。

Sub Last_Row_Range()
Dim nextMonth As Range
Set nextMonth = Sheets("MainTable").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
nextMonth.Select
End Sub

但是,这是我无法弄清楚的。我希望用户能够按下一个按钮 (btnNextMonth),它会自动转到 DateRange 中的最后一行,偏移 1 AND 自动 添加下个月。下面是我想要实现的目标的图片。提前谢谢各位。

【问题讨论】:

    标签: excel dynamic excel-2010 date-range vba


    【解决方案1】:

    尝试使用表格来发挥自己的优势,而不是手动查找最后一行并插入。这是一个例子:

    Sub Last_Row_Range()
        'Get reference to table
        Dim tbl As ListObject
        Set tbl = Sheets("MainTable").Range("MainTable").ListObject
    
        'Insert new row in table
        Dim tblRow As ListRow
        Set tblRow = tbl.ListRows.Add(AlwaysInsert:=True)
    
        'Increment previous date by 1 month and place in new row
        tblRow.Range(1, 1) = DateAdd("m", 1, tblRow.Range(0, 1).Value)
    End Sub
    

    测试

    【讨论】:

    • 感谢@Portland Runner。完美运行!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 2016-01-11
    相关资源
    最近更新 更多