VBA代码自动选择前10列

时间:2016-09-10 13:52:51

标签: excel vba excel-vba macros

参考我之前的问题。

VBA code to add current date in next cell enter image description here

如何从当前日期条目中选择前十天的数据。示例:根据我的第一个屏幕截图让我们说今天的条目将在E列上,我想选择之前的10个条目来创建图表。所以,如果我来tomo,我的条目将在F上,我想选择列F,E,D,C ,,,,

1 个答案:

答案 0 :(得分:0)

保留以前的链接代码,试试这个(未经测试):

在OP澄清之后

编辑

Option Explicit

Sub Update()
    Dim nCols As Long, nOffset As Long

    With Range("A1").CurrentRegion
        With .Offset(, .Columns.Count - 1).Resize(1, 1)
            If .value < Date Then nOffset = 1
            With .Offset(, nOffset)
                .Resize(2, 1).value = Application.Transpose(Array(Date, Application.WorksheetFunction.Subtotal(103, Worksheets("Stock").UsedRange.Columns(1).SpecialCells(XlCellType.xlCellTypeVisible))))
                nCols = IIf(.Column > 10, 10, 10 - .Column - 1)
                .Offset(, -nCols + 1).Resize(, nCols).Select
            End With
        End With
    End With
End Sub
相关问题