Excel VBA – 使用两个条件评估MIN不起作用

我目前正在编写一段代码,在单元格中find两个类别的第一个date。

如果其中一个单元格填充了date,则会忽略它,但如果其他单元格为空,则会运行评估公式。

我遇到的问题是代码公式工作表上,而不是在我得到的代码错误:“运行时错误'1004':应用程序定义或对象定义的错误。

代码在xMinScaff = Evaluate("=MIN(IF('Instruction History'!$D:$D=" & cell.address(False, False) & ",IF('Instruction History'!$E:$E=""Scaffold Req"",'Instruction History'!$H:$H)))") ,我也会假装它会失败xMinWorks = Evaluate("=MIN(IF('Instruction History'!$D:$D=" & cell.address(False, False) & ",IF('Instruction History'!$E:$E<>""Scaffold Req"",'Instruction History'!$H:$H)))")因为它们非常相似。

这是我现在的代码。 谢谢

 Private Sub CommandButton4_Click() Dim cell, cellRange As Range Dim lastRow As Long Dim xMinScaff, xMinWorks As Double lastRow = sheets("Current Asset List").cells(Rows.count, "A").End(xlUp).Row Set cellRange = sheets("Current Asset List").Range("A8:A" & lastRow) For Each cell In cellRange If cell.Offset(0, 24) = "" Then xMinScaff = Evaluate("=MIN(IF('Instruction History'!$D:$D=" & cell.address(False, False) & ",IF('Instruction History'!$E:$E=""Scaffold Req"",'Instruction History'!$H:$H)))") If xMinScaff <> "0" Then cell.offset(0, 24).Value = Format(xMinScaff, "DD/MM/YYYY") End If End If If cell.Offset(0, 25) = "" Then xMinWorks = Evaluate("=MIN(IF('Instruction History'!$D:$D=" & cell.address(False, False) & ",IF('Instruction History'!$E:$E<>""Scaffold Req"",'Instruction History'!$H:$H)))") If xMinWorks <> "0" Then cell.offset(0, 25).Value = Format(xMinWorks, "DD/MM/YYYY") End If End If Next cell End Sub 

看起来你的代码中有一些拼写错误,这可能是你的问题的原因,但由于你没有告诉我们错误在哪里出现,所以可能会有更多的问题。 我纠正了下面的错别字:

 Private Sub CommandButton4_Click() Dim cell, cellRange As Range Dim lastRow As Long Dim xMinScaff, xMinWorks As Double lastRow = sheets("Current Asset List").cells(Rows.count, "A").End(xlUp).Row Set cellRange = sheets("Current Asset List").Range("A8:A" & lastRow) For Each cell In cellRange If cell.Offset(0, 24) = "" Then xMinScaff = Application.Evaluate("=MIN(IF('Instruction History'!$D:$D=" & cell.address(False, False) & ",IF('Instruction History'!$E:$E=""Scaffold Req"",'Instruction History'!$H:$H)))") If xMinScaff <> "0" Then cell.offset(0, 24).Value = Format(xMinScaff, "DD/MM/YYYY") End If End If If cell.Offset(0, 25) = "" Then xMinWorks = Application.Evaluate("=MIN(IF('Instruction History'!$D:$D=" & cell.address(False, False) & ",IF('Instruction History'!$E:$E<>""Scaffold Req"",'Instruction History'!$H:$H)))") If xMinWorks <> "0" Then cell.offset(0, 25).Value = Format(xMinWorks, "DD/MM/YYYY") End If End If Next cell End Sub