我在VBA中遇到了一个公式问题。它适用于条件格式和excel本身,但我不能在VBA级别使用它
这是代码:
Timing-Allow-Origin: value1
Timing-Allow-Origin: value2
Timing-Allow-Origin: value3
其中s_adr_1和s_adr_2是对单元格的引用,因此它看起来像样本(在B1中会有我的测试文本,例如“5-15”,“15”,“5 ... 15” ):
Range("A1").Formula = "=IF(ISERR(FIND(" & Chr(34) & "-" & Chr(34) & ";" & s_adr_1 & ";2))=FALSE;TRUE;IF(ISERR(FIND(" & Chr(34) & "…" & Chr(34) & ";" & s_adr_1 & ";2))=FALSE;TRUE;IF(ISERR(FIND(" & Chr(34) & ".." & Chr(34) & ";" & s_adr_1 & "))=FALSE;TRUE;IF(ISBLANK(" & s_adr_1 & ");FALSE;IF(AND(IF(ISERR(RIGHT(" & s_adr_1 & ")*1);NOT(ISERR(FIND(" & s_adr_2 & ";" & s_adr_1 & ")));TRUE));FALSE;IF(LEFT(" & s_adr_1 & ")=""#"";FALSE;ISERR(FIND(" & s_adr_2 & ";" & s_adr_1 & "))))))))"
A1的预期结果将是:
Range("A1").Formula = "=IF(ISERR(FIND(" & Chr(34) & "-" & Chr(34) & ";" & "B1" & ";2))=FALSE;TRUE;IF(ISERR(FIND(" & Chr(34) & "…" & Chr(34) & ";" & "B1" & ";2))=FALSE;TRUE;IF(ISERR(FIND(" & Chr(34) & ".." & Chr(34) & ";" & "B1" & "))=FALSE;TRUE;IF(ISBLANK(" & "B1" & ");FALSE;IF(AND(IF(ISERR(RIGHT(" & "B1" & ")*1);NOT(ISERR(FIND(" & "C1" & ";" & "B1" & ")));TRUE));FALSE;IF(LEFT(" & "B1" & ")=""#"";FALSE;ISERR(FIND(" & "C1" & ";" & "B1" & "))))))))"
它应该返回TRUE或FALSE。
当我尝试运行代码时出现错误1004 :(
这里有什么问题?当我手动将其粘贴到Excel中时为什么会起作用?
答案 0 :(得分:0)
假设您使用的是英语Excel版本,只需在VBA版本中使用双引号即可。或者几乎做以下事情:
-
Public Sub PrintMeUsefulFormula()
Dim strFormula As String
Dim strParenth As String
strParenth = """"
strFormula = Selection.Formula
strFormula = Replace(strFormula, """", """""")
strFormula = strParenth & strFormula & strParenth
Debug.Print strFormula
End Sub