gpt4 book ai didi

vba - 由于语法错误(录制时)或对象定义错误(运行时)而无法运行 VBA

转载 作者:行者123 更新时间:2023-12-03 08:28:06 25 4
gpt4 key购买 nike

我遇到了和丹尼斯一样的错误 here ,但他提供的答案只是说要删除一些模块或重命名它们。但是,我只有一个模块,重命名它对我没有任何帮助。我什至尝试制作一个全新的工作簿并将宏复制到其中以重新开始,但它也没有做任何事情。 当我在录制宏后去运行宏时,它也会给我一个“应用程序定义或对象定义”的错误。 该公式在宏之外完全可以正常工作,但我确实需要它作为宏的一部分。

这是excel记录的内容,这是我的宏中给我错误的行:

ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,CONCATENATE(RC[-14],"":"",RC[-12]),IF(COUNTA(RC[-15]:RC[-2])=6,CONCATENATE(RC[-14],"":"",RC[-12],"" - "",RC[-10]),IF(COUNTA(RC[-15]:RC[-2])=8,CONCATENATE(RC[-14],"":"",RC[-12],"":"",RC[-10],"" - "",RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,CONCATENATE(RC[-14],"":"",RC[-12],"":"",RC[-10],"":"",RC[-8],"" - """ & _
","""")))))"

它有一个 Range("Q3").Select在此之前,它会激活该细胞。此外,如果您查看它记录的内容与原始公式的对比,您会发现它省略了 K3)RC[-6]), .我试图添加它,但它没有帮助。

原配方:
=IF(COUNTA(B3:O3)=2,C3,IF(COUNTA(B3:O3)=4,CONCATENATE(C3,":",E3),IF(COUNTA(B3:O3)=6,CONCATENATE(C3,":",E3," - ",G3),IF(COUNTA(B3:O3)=8,CONCATENATE(C3,":",E3,":",G3," - ",I3),IF(COUNTA(B3:O3)=10,CONCATENATE(C3,":",E3,":",G3,":",I3," - ",K3),"")))))

我还尝试了没有 concatenate 的公式,但它也没有帮助。它在下面:
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"

编辑:这是上面的代码,P3 公式工作正常:
 Range("P3").Select
ActiveCell.FormulaR1C1 = _
"=INDEX(RC[-14]:RC[-1],1,IF(COUNT(RC[-14]:RC[-1])=1,COUNT(RC[-14]:RC[-1]),COUNT(RC[-14]:RC[-1])*2-1))"
Range("P3").Select
Range("P3").AutoFill Destination:=Range("P3:P" & LastRow)


Range("Q3").Select
ActiveCell.FormulaR1C1 = _
"=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"
Range("Q3").Select
Range("Q3").AutoFill Destination:=Range("Q3:Q" & LastRow)

我正在使用 Microsoft Office 2013,并且有一个 ASAP Utilites 插件,如果这有帮助的话,它以前从未给我带来过问题。

最佳答案

公式本身包含错误:

ActiveCell.FormulaR1C1 = "=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8]),IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"

应该
ActiveCell.FormulaR1C1 = "=IF(COUNTA(RC[-15]:RC[-2])=2,RC[-14],IF(COUNTA(RC[-15]:RC[-2])=4,RC[-14]&"":""&RC[-12],IF(COUNTA(RC[-15]:RC[-2])=6,RC[-14]&"":""&RC[-12]&"" - ""&RC[-10],IF(COUNTA(RC[-15]:RC[-2])=8,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"" - ""&RC[-8],IF(COUNTA(RC[-15]:RC[-2])=10,RC[-14]&"":""&RC[-12]&"":""&RC[-10]&"":""&RC[-8]&"" - ""&RC[-6],"""")))))"

...RC[-8]),IF...不应该有 ) !

如果你删除它,宏运行不会失败。
仍然:我建议改变你的公式
=IF(COUNTA(B3:O3)=2,C3,IF(COUNTA(B3:O3)=4,CONCATENATE(C3,":",E3),IF(COUNTA(B3:O3)=6,CONCATENATE(C3,":",E3," - ",G3),IF(COUNTA(B3:O3)=8,CONCATENATE(C3,":",E3,":",G3," - ",I3),IF(COUNTA(B3:O3)=10,CONCATENATE(C3,":",E3,":",G3,":",I3," - ",K3),"")))))


=IFERROR(CHOOSE(COUNTA(B3:O3)/2,C3,C3&":"&E3,C3&":"&E3&" - "&G3,C3&":"&E3&":"&G3&" - "&I3,C3&":"&E3&":"&G3&":"&I3&" - "&K3),"")

哪个更短;)

对于“错误”记录的部分:如果excel将您的公式“翻译”为R1C1,并且字符串被切割成多行,那么有时会丢失部分。对于这种情况(如果您注意到公式被剪切为多行),只需选择单元格并在即时窗口中运行: ?ActiveCell.FormulaR1C1得到R1C1中的公式。但不要忘记加倍所有 " .

关于vba - 由于语法错误(录制时)或对象定义错误(运行时)而无法运行 VBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38018547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com