简体   繁体   English

如何使用if-then语句创建活动/非活动状态并遍历所有行VBA / Excel

[英]how to create active/inactive status using an if-then statement and looping through all rows VBA/Excel

I am trying to create a project management macros and I have coded many parts of it, but I am having trouble with the last piece below. 我正在尝试创建一个项目管理宏,并且已经对它的很多部分进行了编码,但是在下面的最后一部分中遇到了麻烦。

Each task has a start date (Column D), end date (Column E),and a status (Column K). 每个任务都有一个开始日期(D列),结束日期(E列)和一个状态(K列)。 My initial goal was to check if any of the days within "this week" would fall within the start date and end date for that particular row. 我的最初目标是检查“本周”内的某天是否在该特定行的开始日期和结束日期之内。 I completed this goal and now I would like to loop this formula/code and create a status for every row based on the start date and end date for every individual row. 我完成了这个目标,现在我想循环此公式/代码,并根据每行的开始日期和结束日期为每行创建一个状态。

There are an infinite number of rows, because each project is different and I using this macros as a template. 行数是无限的,因为每个项目都是不同的,并且我使用此宏作为模板。

Does anyone know how to loop this formula through each row until there are no more rows with data in them? 有谁知道如何在每行中循环使用此公式,直到不再有包含数据的行?

Thank you so much for your help in advance! 非常感谢您的提前帮助!

Sub PM_Schedule() 

'mark as active if this week falls within range D:E

Dim bk As Integer

    Application.ScreenUpdating = False

    ' Set numrows = number of rows of data.
    NumRows = Range("$D2", Range("$D2").End(xlDown)).Rows.Count

    ' Select cell D2.
    Range("$D2").Select

    ' Establish "For" loop to loop "numrows" number of times.
    For x = 1 To NumRows

    Dim D As Integer
    Dim N As Date

    D = Weekday(Now)
    Thismonday = Now() + (2 - D)
    ThisTuesday = Now() + (3 - D)
    ThisWednesday = Now() + (4 - D)
    ThisThursday = Now() + (5 - D)
    ThisFriday = Now() + (6 - D)

    StartDate = xWs.Range("$D2").Value
    EndDate = xWs.Range("$E2").Value

        If Thismonday >= StartDate And Thismonday <= EndDate Then
            Status = "Active"
        ElseIf ThisTuesday >= StartDate And ThisTuesday <= EndDate Then
            Status = "Active"
        ElseIf ThisWednesday >= StartDate And ThisWednesday <= EndDate Then
            Status = "Active"
        ElseIf ThisThursday >= StartDate And ThisThursday <= EndDate Then
            Status = "Active"
        ElseIf ThisFriday >= StartDate And ThisFriday <= EndDate Then
            Status = "Active"
        Else
            Status = "Not Active"
        End If

        xWs.Range("$K2").Value = Status

    Next

        ' Selects cell down 1 row from active cell.
          ActiveCell.Offset(1, 0).Select

      Next

      Application.ScreenUpdating = True

End Sub

Untested, but something like this should be close: 未经测试,但是类似这样的东西应该很接近:

Sub PM_Schedule()

    'mark as active if this week falls within range D:E
    Dim xWS As Worksheet, c As Range
    Dim ThisMonday, ThisFriday, StartDate, EndDate, Status

    Set xWS = ActiveSheet
    ThisMonday = Date + (2 - Weekday(Date)) '<< use Date, not Now
    ThisFriday = ThisMonday + 4

    Application.ScreenUpdating = False

    'It's usually safer to use End(xlUp) than xlDown
    For Each c In xWS.Range(xWS.Range("D2"), _
                            xWS.Cells(xWS.Rows.Count, "D").End(xlUp)).Cells

        StartDate = c.EntireRow.Cells(4).Value 'D
        EndDate = c.EntireRow.Cells(5).Value   'E

        c.EntireRow.Cells(11).Value = IIf(StartDate <= ThisFriday And EndDate >= ThisMonday, _
                                        "Active", "Not active")
    Next
    Application.ScreenUpdating = True

End Sub

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

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