简体   繁体   中英

Excel VBA Macro: Autofill a list in a Weekday frequency

With this macro I am able to insert a new row at the bottom of a daily time-series data. The macro performs well, but even though I specified to fill the series with Weekdays and avoid weekends, it still not do so and fills with all the days of the week.

Any suggestions on what I might be missing?

Please also see the screenshot for a better understanding.

Thank a lot.

  Sub Weekday_Data_Update()

   Range("A2").Select
   Selection.End(xlDown).Select
   ActiveCell.Offset(-1, 0).Range("A1").Select
   Range(Selection, Selection.End(xlToRight)).Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Resize(3).Select
   Selection.DataSeries Rowcol:=xlColumns, Type:=xlAutoFill, Date:=xlWeekday,  _
   Trend:=False

End sub

Example of how the macro is filling the dates wrongly

宏如何错误地填充日期的示例

I am confused by all your up, down, across movement. To simply extend dates across a range only using weekdays would have syntax such as follows:

Option Explicit

Public Sub Test()
    With Worksheets("Sheet1")
        .Range("A2").AutoFill Destination:=.Range("A2:A7"), Type:=xlFillWeekdays
    End With
End Sub

You can construct a fill to last used cell as follows:

.Range("A2").AutoFill Destination:=.Range(.Cells(2, "A"), .Cells(.Cells.SpecialCells(xlLastCell).Row, "A")), Type:=xlFillWeekdays

Just to break it down, the below code will print ONLY the Weekdays

Sub Weekday_Data_Update()


Dim startRange As Range
Dim stopRange As Range

'Specify the cell where the date starts
Set startRange = Sheets("Sheet1").Range("A2")

'Specify the cell until which you want weekdays to be displayed
Set stopRange = Sheets("Sheet1").Range("A2:A6")

startRange.Select
Selection.AutoFill Destination:=stopRange, Type:=xlFillWeekdays



End Sub

在此处输入图片说明

PS : Not sure why you many range selections in your code.

According to MSDN, Date:=xlWeekday is only applicable when you use Type:=xlChronological

So, try

Selection.DataSeries Rowcol:=xlColumns, Type:=xlChronological, Date:=xlWeekday,  _
Trend:=False

Refer to MSDN help for further details

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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