VBAmacros从两个date列减去工作日

目前我正在努力减去两天之间使用Networkdays.Intl两个列平日。

但是错误显示

所需对象。

我需要如何解决这个问题? 我实际上不确定我使用Networkdays.Intl的方式是否正确。

这是我的代码:

Private Sub MinusWeekDays() Dim InitialRow As Long Dim EndRow As Long Dim lrow As Long Dim CurrentSheet As Worksheet Set CurrentSheet = Excel.ActiveSheet InitialRow = CurrentSheet.UsedRange.Cells(1).Row EndRow = CurrentSheet.UsedRange.Rows(CurrentSheet.UsedRange.Rows.count).Row For lrow = EndRow To InitialRow Step -1 CurrentSheet.Cells(lrow, "AH").Value = NetworkDays.Intl((CurrentSheet.Cells(lrow, "W").Value), (CurrentSheet.Cells(lrow, "V").Value), (CurrentSheet.Cells(lrow, "AG").Value)) Next lrow End Sub 

我也试着用Dim intdays作为Integer来replaceCurrentSheet.Cells(lrow,“AG”)Value)直接使用'11',intdays = 11因为避免了需要创build一个只有11值的列,但似乎失败了。

您似乎错过了从调用NETWORKDAYS.INTL函数的WorksheetFunction对象 。 我已经将重复调用的工作表移动到了周围的With … End With语句中 。

 WITH CurrentSheet For lrow = EndRow To InitialRow Step -1 .Cells(lrow, "AH").Value = WORKSHEETFUNCTION.NetworkDays_Intl(.Cells(lrow, "W"), .Cells(lrow, "V"), 11) Next lrow END WITH 

每个周末的周末types是否真的不同,你不能只把一个数字放在第三个参数中而不是.Cells(lrow, "AG") ? 检查NETWORKDAYS.INTL函数的官方文档,以确保您提供了正确的参数。