我下面的代码有两部分。第一部分基本上将一个单元格设置为一个日期,即从现在起6个月。这部分起作用了,但我对第二部分有困难。
第二部分是过滤表之后,我想将H列中的每个单元格(它是日期值)与第一部分中的未来日期进行比较。如果列H中的日期是未来日期之后的,则该行的单元格AI将设置为“是”。
Set Database = ThisWorkbook.Worksheets("Sheet1")
Dim i As Long, LastRow As Long
LastRow = Database.Cells(Rows.count, "A").End(xlUp).Row
With Database.Range("AI1")
'Adds 6 months to today's date in cell AI1 in sheet
.Formula = "=EDATE(Today(),6)"
'Converts that date to 1st day of the month (e.g. 17/01/2020 is converted to 01/01/2020
.Value = DateSerial(Year(Range("AI1")), Month(Range("AI1")), 1)
End With
For i = 2 To LastRow
'This line throws a run-time error 13: type mismatch
If Cells(i, "H").SpecialCells(xlCellTypeVisible) > Database.Range("AI1") Then
Cells(i, "AI").Value = "Yes"
End If
Next i发布于 2021-02-17 07:12:04
下面应该可以工作,注意它可以在第一个循环迭代中替换AI1中的值。你不确定你真的想那样吗?
Dim c As Range, vis As Range, dt as Date
'calculate cutoff date
dt = Application.EDate(Date, 6)
dt = DateSerial(Year(dt), Month(dt), 1)
On Error Resume Next 'ignore error if no visible cells
Set vis = database.Range("H1:H" & LastRow).SpecialCells(xlCellTypeVisible)
On Error GoTo 0 'stop ignoring errors
If Not vis Is Nothing Then 'any visible cells ?
For Each c In vis.Cells
If c.Value > dt Then c.EntireRow.Range("AI1").Value = "Yes"
Next c
End Ifhttps://stackoverflow.com/questions/66235540
复制相似问题