VBA code to auto select previous 10 columns

狂风中的少年 提交于 2020-01-05 04:55:17

问题


Referring from my previous question.

VBA code to add current date in next cell

How do I select the previous ten days data from current date entry. Example :As per my first screenshot Lets say i get the today entry will be on Column E and I want to select previous 10 entries to create a graph. So, if I come tomo, my entry will be on F and I want to select column F,E,D,C,,,,


回答1:


Keeping you previous code from link, try this (untested):

edited after OPs clarifications

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


来源:https://stackoverflow.com/questions/39426748/vba-code-to-auto-select-previous-10-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!