繁体   English   中英

Excel VBA宏:以工作日频率自动填充列表

[英]Excel VBA Macro: Autofill a list in a Weekday frequency

使用此宏,我可以在每日时间序列数据的底部插入新行。 该宏的效果很好,但是即使我指定用“ 工作日”填充该系列并避免使用“周末”,它仍然不能这样做,而是用一周中的所有天填充。

关于我可能会缺少的任何建议吗?

另请参阅屏幕截图,以更好地理解。

非常感谢。

  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

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

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

您的上下运动都让我感到困惑。 仅使用工作日将日期简单地扩展到一个范围内,其语法如下:

Option Explicit

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

您可以按以下方式构造填充到最后使用的单元格:

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

只是为了分解,下面的代码在工作日打印

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 :不确定为什么要在代码中选择很多范围。

根据MSDN, Date:=xlWeekday仅在您使用Type:=xlChronological时适用。

所以,尝试

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

有关更多详细信息,请参考MSDN帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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