在VBA中产生星期六的数量

我是新来的编码,并希望帮助编写一个macros的代码生成每个星期六的数量,每个月的第一个date已被列为Excel文件中的范围。 该文件应该如下所示

Month Weeks 1/1/2016 5 2/1/2016 4 3/1/2016 4 4/1/2016 5 5/1/2016 4 6/1/2016 4 7/1/2016 5 8/1/2016 4 9/1/2016 4 10/1/2016 5 11/1/2016 4 12/1/2016 4 

这将工作。 您需要在单元格前面添加表格(“SheetName”)。

 Sub CountSaturdays() LR = Cells(Rows.Count, 1).End(xlUp).Row For x = 2 To LR StDt = Cells(x, 1) EndDt = WorksheetFunction.EoMonth(Cells(x, 1), 0) For Dt = StDt To EndDt If WorksheetFunction.Weekday(Dt) = 7 Then Sat = Sat + 1 End If Next Dt Cells(x, 2) = Sat Sat = 0 Next x End Sub 

我不清楚为什么你需要这个VBA。

作为工作表公式,

 =NETWORKDAYS.INTL(A2, EOMONTH(A2, 0), "1111101")