简体   繁体   English

VBA中的工作日功能

[英]Weekday function in VBA

I'm trying to accomplish the below task in VBA using weekday function : 我正在尝试使用平日功能在VBA中完成以下任务:

What I want to do is: 我想做的是:

  1. I have dates in col K and this code should run only on weekday dates and not on weekend dates. 我在K列中有日期,并且此代码应仅在工作日日期而不是周末日期运行。

  2. I need to add additional text along with "Moved to SA (Compatibility Reduction)", let's say "Moved to SA (Failure)" . 我需要添加其他文本以及“已移至SA(减少兼容性)”,比如说“已移至SA(失败)”。 So, if col P has either "Moved to SA (Compatibility Reduction)" or "Moved to SA (Failure)" the coloring should happen. 因此,如果col P具有“已移至SA(减少兼容性)”或“已移至SA(失败)”,则应该发生着色。

  3. This code should run only in sheet "Latency" 此代码应仅在工作表“延迟”中运行

I have the below code but it's throwing an error message of : 我有下面的代码,但它抛出一条错误消息:

wrong number of arguments or invalid property assignment

My code: 我的代码:

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

here's your Sub code refactored for what you asked 这是您要求的重构后的子代码

I also changed its previous name to WeekdayCheck() s not to hide VBA WeekDay() function 我也将其以前的名称更改为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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM