我想创建一个包含一列的表,该列包含日期范围内所有工作日的日期。我已经在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
'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
如果我希望获得超过几个月的日期范围,那么当前的do-while循环太慢了。有什么方法可以修改它以使其更快地工作吗?
答案 0 :(得分:1)
如果您有一个庞大的工作表...关闭自动重新计算