平日function在VBA中

我正在尝试使用weekday函数在VBA中完成以下任务:

我想要做的是:

  1. 我有col K的date,这个代码应该只在周date运行,而不是周末date。

  2. 我需要添加额外的文本以及“移到SA(兼容性降低)”,让我们说“移到SA(失败)”。 因此,如果col P已经“移到SA(兼容性降低)”或“移到SA(失败)”,则着色应该发生。

  3. 这段代码只能运行在“延迟”

我有下面的代码,但它抛出的错误信息:

wrong number of arguments or invalid property assignment 

我的代码:

 Sub Weekday() Dim r, LastRow, RemainingDay As Double LastRow = Range("M:O").Cells(Rows.count, "A").End(xlUp).Row Application.ScreenUpdating = False For r = 2 To LastRow RemainingDay = 0 If Weekday(Range("K" & r).Value, vbMonday) = 2 Then Select Case True Case InStr(Range("P" & r).Text, "Moved to SA (Compatibility Reduction)") > 0, _ InStr(Range("P" & r).Text, "Moved to SA (Failure)") > 0 If Range("M" & r) - RemainingDay >= 1 Then Range("M" & r).Cells.Font.ColorIndex = 3 Else Range("M" & r).Cells.Font.ColorIndex = 0 End If End Select End If End If Next r End Sub 

这里是你的子代码重构你所问

我也改变了以前的名字WeekdayCheck()隐藏 VBA WeekDay()函数

 Sub WeekdayCheck() Dim r As Long, LastRow As Long Dim RemainingDay As Double '<--| you seem no to use it! if so get rid of it With Worksheets("Latency") '<--| reference worksheet "Latency" LastRow = .Cells(.Rows.Count, "A").End(xlUp).row '<--| get row index of its column A last not empty cell Application.ScreenUpdating = False For r = 2 To LastRow RemainingDay = 0 '<--| you seem no to use it! if so get rid of it If Weekday(.Range("K" & r).Value, vbSaturday) > 2 Then '<--| having 'Weekday()' function starting from "Saturday", it'll return numbers from 3 to 7 for not weekend weekdays Select Case True Case InStr(.Range("P" & r).Text, "Moved to SA (Compatibility Reduction)") > 0, _ InStr(.Range("P" & r).Text, "Moved to SA (Failure)") > 0 If .Range("M" & r) - RemainingDay >= 1 Then .Range("M" & r).Cells.Font.ColorIndex = 3 Else .Range("M" & r).Cells.Font.ColorIndex = 0 End If End Select End If Next r End With End Sub