简体   繁体   English

使用Excel VBA将Excel公式(Excel格式)粘贴到单元格中吗?

[英]Paste excel formula (excel format) in a cell using excel VBA?

How do I insert or paste the large excel formula in a specific cell in excel formula formate only. 如何仅在excel公式formate中的特定单元格中插入或粘贴大型excel公式。 My project has large excel table to calulate no. 我的项目中有一个很大的Excel表,无法计算。 days left and it is linked with current date and time. 还剩几天,它与当前日期和时间链接。 formula has to be inserted in column whenever i submitted the data from data entry form. 每当我从数据输入表单提交数据时,都必须在列中插入公式。 It automatically collects the values from different cells in a current sheet and calculates. 它会自动从当前工作表的不同单元格中收集值并进行计算。
But here i getting "compile error, expected end of statement" at formula line ie., at double quatotions "". 但是在这里,我在公式行(即在双四元数“”处)得到“编译错误,语句的预期结尾”。

I could write directly in excel and drage 我可以直接在excel和drage中编写
or 要么
I could write vba code for the above calculation but due to my project requirement, i have to be inserted the formula. 我可以为上述计算编写vba代码,但是由于我的项目要求,我必须插入公式。

Is there any way to insert the formula???? 有什么办法可以插入公式???? i'm using excel 2016 我正在使用excel 2016

Set Fcell = formulaWks.Range("O7")

'formula = "=$A1+$B1"  ' example for testing
Formula = "=IF(YEAR(NOW())=$W$3,IF(ISBLANK($G7),"",IFERROR(IF(DATEDIF(TODAY(),$N7,"y")=0,"",DATEDIF(TODAY(),$N7,"y")&" y ")&IF(DATEDIF(TODAY(),$N7,"ym")=0,"",DATEDIF(TODAY(),$N7,"ym")&" m ")&IF(DATEDIF(TODAY(),$N7,"md")=0,"",DATEDIF(TODAY(),$N7,"md")&" d"),"wrong date")),"Package completed")"
Fcell = ActiveCell.formula

You need to escape double quotes from the string first by adding double quotes twice in the string like this - ISBLANK($G7),"""" 您需要先通过在字符串中两次添加双引号来对字符串中的双引号进行转义ISBLANK($G7),""""

Then use formula like this 然后使用这样的公式

Range("O7").Formula = "[Your formula with escaped double quotes]" 

Try the code below, it will work with your basic formula that you wanted to test. 尝试下面的代码,它将与您要测试的基本公式一起使用。

Option Explicit

Sub InsertFormula()

Dim formulaWks As Worksheet
Dim Fcell As Range
Dim FormulaString   As String

' modify "Sheet1" to your sheet's name     
Set formulaWks = Worksheets("Sheet1")
Set Fcell = formulaWks.Range("O7")

FormulaString = "=$A1+$B1"  ' example for testing
Fcell.Formula = FormulaString

End Sub

Regarding your "LONG" formula, the formula string below passes: 关于您的“长”公式,下面的公式字符串通过:

FormulaString = "=IF(YEAR(NOW())=$W$3,IF(ISBLANK($G7)," & Chr(34) & Chr(34) & ",IFERROR(IF(DATEDIF(TODAY(),$N7," & Chr(34) & "y" & Chr(34) & ")=0," & Chr(34) & Chr(34) & ",DATEDIF(TODAY(),$N7," & Chr(34) & "y" & Chr(34) & ")&" & Chr(34) & " y " & Chr(34) & ")" & _
                "&IF(DATEDIF(TODAY(),$N7," & Chr(34) & "ym" & Chr(34) & ")=0," & Chr(34) & Chr(34) & ",DATEDIF(TODAY(),$N7," & Chr(34) & "ym" & Chr(34) & ")&" & Chr(34) & " m " & Chr(34) & ")" & _
                "&IF(DATEDIF(TODAY(),$N7," & Chr(34) & "md" & Chr(34) & ")=0," & Chr(34) & Chr(34) & ",DATEDIF(TODAY(),$N7," & Chr(34) & "md" & Chr(34) & ")&" & Chr(34) & " d" & Chr(34) & ")," & _
                Chr(34) & "wrong date" & Chr(34) & "))," & Chr(34) & "Package completed" & Chr(34) & ")" 

Debug.Print FormulaString ' for debug, to see the Formula string in the immediate window

Note: Final version of the "long" formula has been edited in by YowE3K - if it doesn't work, blame me (ie YowE3K) not Shai. 注意:“长”公式的最终版本已由YowE3K编辑-如果不起作用,请怪我(即YowE3K)而不是Shai。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM