我目前正在编写一段代码,在单元格中找到两个类别的第一个日期。
如果其中一个单元格填入了日期,则忽略它,但如果另一个单元格为空,则运行评估公式。
我遇到的问题是代码公式在工作表上有效,但在我得到的代码中没有错误:"运行时错误' 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
答案 0 :(得分:0)
您的代码中似乎存在一些拼写错误,这可能是导致问题的原因,但由于您没有告诉我们错误发生的位置,因此可能会出现更多问题。我已经纠正了下面的拼写错误:
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