在date范围(Excel-VBA)之间添加工作日栏的快速方法

我想创build一个列,其中包含date范围内所有星期几的date。 我在excel-vba中实现了一些代码(看起来像这样):

主要

Sub columnofworkdaystest() 'init Dim ws As Worksheet Set ws = ThisWorkbook.Worksheets("Sheet1") ws.Range("A2").Value = "Date" 'Set the table ws.ListObjects.Add(xlSrcRange, Range("A2:A4"), , xlYes).Name = "table1" Dim hcrtbl As ListObject Set hcrtbl = ws.ListObjects("table1") 'get dates Dim startdate, enddate, runningdate As Date startdate = Date - 20 enddate = Date runningdate = startdate 'insert rows - this part is really slow ws.Range("A2").Offset(1, 0).Value = runningdate Do While runningdate <= enddate hcrtbl.ListColumns("Date").DataBodyRange(hcrtbl.ListRows.Count - 1, 1).Value = runningdate hcrtbl.ListRows.Add runningdate = GetNextWorkDay(runningdate) Loop End Sub 

GetNextWorkDay

 'I copied this from here: http://www.nullskull.com/q/10252552/find-next-working-day-vba.aspx Function GetNextWorkDay(dtTemp As Date) GetNextWorkDay = dtTemp + IIf(Weekday(dtTemp) > 5, 9 - Weekday(dtTemp), 1) End Function 

如果我想要获得几个月的date范围,目前的do-while循环太慢了。 有什么办法可以修改这个,使其工作更快?

如果你有一个巨大的工作表…closures自动重新计算