Heyo,我正在尝试使用 VBA 将(非常)长的公式插入工作页中。我不断收到“应用程序定义/对象定义”错误。
希望有一种方法可以实现这项工作,或者只是优化一般公式,因为它很愚蠢,但我找不到解决方法。 该公式实际上会检查每个单元格中的特定短语,然后询问其下方 2 行单元格的值。
Dim sFrm As String
sFrm = "=IF(Invoice_Statement!A20=""Total Amount Due: "",Invoice_Statement!A22,IF(Invoice_Statement!A21=""Total Amount Due: "",Invoice_Statement!A23,IF(Invoice_Statement!A22=""Total Amount Due: "",Invoice_Statement!A24,IF(Invoice_Statement!A23=""Total Amount Due: "",Invoice_Statement!A25,IF(Invoice_Statement!A24=""Total Amount Due: "",Invoice_Statement!A26,IF(Invoice_Statement!A25=""Total Amount Due: "",Invoice_Statement!A27,IF(Invoice_Statement!A26=""Total Amount Due: "",Invoice_Statement!A28," & _
"IF(Invoice_Statement!A27=""Total Amount Due: "",Invoice_Statement!A29,IF(Invoice_Statement!A28=""Total Amount Due: "",Invoice_Statement!A30,IF(Invoice_Statement!A29=""Total Amount Due: "",Invoice_Statement!A31,IF(Invoice_Statement!A30=""Total Amount Due: "",Invoice_Statement!A32,IF(Invoice_Statement!A31=""Total Amount Due: "",Invoice_Statement!A33,IF(Invoice_Statement!A32=""Total Amount Due: "",Invoice_Statement!A34,IF(Invoice_Statement!A33=""Total Amount Due: "",Invoice_Statement!A35,IF(Invoice_Statement!A34=""Total Amount Due: "",Invoice_Statement!A36," & _
"IF(Invoice_Statement!A35=""Total Amount Due: "",Invoice_Statement!A37,IF(Invoice_Statement!A36=""Total Amount Due: "",Invoice_Statement!A38,IF(Invoice_Statement!A37=""Total Amount Due: "",Invoice_Statement!A39,IF(Invoice_Statement!A38=""Total Amount Due: "",Invoice_Statement!A40,IF(Invoice_Statement!A39=""Total Amount Due: "",Invoice_Statement!A41,IF(Invoice_Statement!A40=""Total Amount Due: "",Invoice_Statement!A42,"")))))))))))))))))))))"
Range("J3").Value = sFrm
我想出的让函数更高效的答案是
=VLOOKUP(M3&"*",Invoice_Statement!A:D,1,FALSE)
M3 包含我要查找的实际行的前 5 个字符(前 5 个始终相同,其余各不相同)
蒂姆的解决方案也有效,如果出现类似问题,我一定会记住这一点
=IFERROR(OFFSET(Invoice_Statement!A20,1+MATCH("Total Amount Due: ",Invoice_Statement!A20:A40,0),0),"")