简体   繁体   English

具有日期值> = Date的Excel VBA联合行

[英]Excel VBA Union Rows with date value >= Date

I am trying to write a macro that populates an email with rows in a range if the date cell (column H) in the row is greater than or equal to the current date. 我正在尝试编写一个宏,如果行中的日期单元格(列H)大于或等于当前日期,则填充包含范围内行的电子邮件。

The below code populates an email with seemingly random rows. 以下代码使用看似随机的行填充电子邮件。

Dim rng As Range
Dim row As Range
Dim rng2 As Range

Set rng = Range("B52:I79")

For Each row In rng.Rows
    If row.Columns("H") >= Date Then
        If Not rng2 Is Nothing Then
            Set rng2 = Union(rng2, row)
        Else
            Set rng2 = row
        End If
    End If
Next

Any help would be great! 任何帮助都会很棒!

Since your range starts from column B, if your date is in column H then you need to refer to it as column G as below: 由于您的范围从B列开始,如果您的日期在H列中,那么您需要将其称为G列,如下所示:

Dim rng As Range
Dim row As Range
Dim rng2 As Range

Set rng = Range("B52:I79")

For Each row In rng.Rows
    If row.Columns("G") >= Date Then
        If Not rng2 Is Nothing Then
            Set rng2 = Union(rng2, row)
        Else
            Set rng2 = row
        End If
    End If
Next

Instead of refering to columns with letters in a range, use numbers. 不使用范围内的字母引用列,而是使用数字。 That way they are more obvious relatively. 这样他们相对来说就更明显了。

range from b to i with column h is actually going to return i... call it column 7 though and you get less confusing results. 范围从b到i,列h实际上将返回i ...虽然称之为第7列,但您得到的混淆结果更少。

Also, to debug this, step through your code and send the match results to a variable in addition to the the union command. 此外,要调试此操作,请逐步执行代码并将匹配结果发送到除union命令之外的变量。

That way you can see in your locals window each time it matches and you can ensure that the info that you want to match on is the info that is being matched on. 这样,您可以在每次匹配时在本地窗口中看到,并且您可以确保要匹配的信息是正在匹配的信息。

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

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