0

I am trying to create a macro that will automatically delete rows from a raw data file based on multiple criteria. I have been able to create a macro that will delete rows where certain columns have blank values, but I also need to delete rows that have a date that is equal or greater than a date contained in a specific cell in another worksheet. I have been all over the internet but have not found an explanation that addresses this particular case or one similar enough for me to figure it out. I am working on automating a number of other reports, and this is the final piece that I need to be able to finish several of them. Here is the code I am trying to use.

 Sub Deletedatesbeyondreportmonth()

 Application.ScreenUpdating = False

 With ActiveSheet

    .AutoFilterMode = False

    .Columns("E").AutoFilter Field:=1, Criteria1:=">= sheets("Sheet2").Range("C1")"

    .AutoFilter.Range.Offset(1, 0).EntireRow.Delete

    .AutoFilterMode = False

End With

End Sub

If I replace the criteria sheets("Sheet2").Range("C1") with the actual value that is contained in the specified cell the macro works, however I need it to be able to run based off the value contained in that cell. I have played with some syntax variations and got different error messages, but haven't been able to figure it out. If I run this as is, it highlights the word Sheet2 on the criteria line and says:

Compile Error expected: End of statement

4

2 回答 2

0

这可能是嵌套的双引号。尝试使用双双引号,例如:

.Columns("E").AutoFilter Field:=1, Criteria1:=">= sheets(""Sheet2"").Range(""C1"")"

这里的例子。

于 2012-12-24T05:35:54.083 回答
0

当您用引号封装 VBA 逻辑时,您实际上对字符串进行了硬编码,因此它将使用Sheets("Sheet2").Range("C1")过滤器中的文本而不是评估 Range 值。您需要使用以下内容:

.Columns("E").AutoFilter Field:=1, Criteria1:=">=" & Sheets("Sheet2").Range("C1")
于 2012-12-24T10:37:05.907 回答