vba formulaR1c1连接

时间:2016-06-27 19:35:35

标签: vba excel-vba excel

我想要一个具有此公式的单元格来计算两个不同的时间。

EX:

2016/6/22 10:19:26 AM, 6/24/2016 10:33:57 AM

如何在vba中写这个? 我试着在R1C1中做到:

Range("A1").FormulaR1C1 = "=INT(RC[4]-RC[3])" & " days, " & "Hour(RC[4]-RC[3])" & " hours, " & "Minute(RC[4]-RC[3])" & " minutes and " & "Second(RC[4]-RC[3])" & " seconds" 

它显示了应用程序定义或对象定义的错误。

我想要这样的结果:

2 days, 0 hours, 14 minutes and 31 seconds

2 个答案:

答案 0 :(得分:1)

问题是实际公式中的引号需要转义(在VBA中)并且有一些引号放错位置。

Range("A1").FormulaR1C1 = "=INT(RC[4]-RC[3]) & "" days, "" & Hour(RC[4]-RC[3]) & "" hours, "" & Minute(RC[4]-RC[3]) & "" minutes and "" & Second(RC[4]-RC[3]) & "" seconds""

答案 1 :(得分:1)

目前,您要为excel提供的公式是

=INT(RC[4]-RC[3])days, Hour(RC[4]-RC[3]) hours, Minute(RC[4]-RC[3]) minutes and Second(RC[4]-RC[3]) seconds

您可以调用CONCATENATE函数。所以你的代码看起来像这样。

Range("A1").FormulaR1C1 = "=CONCATENATE(INT(RC[4]-RC[3]), "" days, "", Hour(RC[4]-RC[3]), "" hours, "", Minute(RC[4]-RC[3]), "" minutes and "" ,Second(RC[4]-RC[3]), "" seconds"")"