2017-10-06 108 views
0

各位程序员,您好!Excel中的公式转化为VBA代码

我是新来的VBA,并应对现有的Excel模板做出很大的改变... 现在我的问题是,虽然试图从excel中导出一个公式(它应该稍后点击按钮),我我无法使用这个简单的步骤:

Sub InsertTodaysDate() 
    ' This macro will put today's date in cell A1 on Sheet1 
    Sheets("Sheet1").Select 
    Range("A1").Select 
    Selection.Formula = "=text(now(),""mmm dd yyyy"")" 
    Selection.Columns.AutoFit 
End Sub 

这并不工作,但插入我的伤心地大公式

Sub InsertTodaysDate() 
    Sheets("Sheet1").Select 
    Range("A1").Select 
     Selection.FormulaLocal = "=WENN(LÄNGE('Step 2 - Add Contact Informatio'!A4)>100;""too many characters"";"" 
WENN('Step 2 - Add Contact Informatio'!A4="""";""Email is mandatory""; 
WENN(ISTZAHL(FINDEN(""!"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""*"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN("":"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""="";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""`"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""\""; 
'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""]"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""["";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""}"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""{"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""´"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""?"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed"";WENN(ISTZAHL(FINDEN("")""; 
'Step 2 - Add Contact Informatio'!A4));""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""("";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed"";WENN(ISTZAHL(FINDEN(""/"";'Step 2 - Add Contact Informatio'!A4));""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""&"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""%"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""$"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""§"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""~"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""“"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""^"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""°"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""<"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN("" "";'Step 2 - Add Contact Informatio'!A4)); 
""spaces are not allowed""; 
WENN(ISTZAHL(FINDEN(""#"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN(""'"";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN("","";'Step 2 - Add Contact Informatio'!A4)); 
""special characters are not allowed""; 
WENN(ISTZAHL(FINDEN("">"";'Step 2 - Add Contact Informatio'!A4));""special characters are not allowed""; 
WENN(ISTFEHLER(D8);""error"";WENN(D8=FALSCH;""error"";""ok"")))))))))))))))))))))))))))))))))" 
End Sub 

当我得到一个语法错误,我甚至试图逃避“”与\“\”但这只适用于公式的一半。有人知道问题是什么吗?

+0

真正的问题是为什么你需要这么长的公式。无疑有更好的方法。也许从** codereview.stackexchange **这样的问题开始** – CallumDA

+0

@CallumDA很好,因为我的老板说,它似乎已经工作过,他们想继续使用它 –

+0

我能理解 - 羞辱你的老板不是'尽管开放给更好的解决方案! – CallumDA

回答

0

我真的很感谢你的帮助,可悲的是他们都没有工作。但我能说服我的老板使用正则表达式。再次感谢,@你的方法确实奏效,但我不知道如何将其标记为答案

0

我试图将其从FormulaLocal转换为Formula,并将其放入“常规”VBA,至少它似乎在我的工作。我会把每个IF放在一个单独的行上,但在经过25 _之后,它会变得疯狂。 (如果我错译任何的功能,让我知道!)

Sub InsertTodaysDate() 

Sheets("Sheet1").Select 
Range("A1").Select 

Selection.Formula = _ 
"=IF(LEN('Step 2 - Add Contact Informatio'!A4)>100," & """" & "too many characters" & """" & "," & _ 
"IF('Step 2 - Add Contact Informatio'!A4=" & """" & """" & "," & """" & "Email is mandatory" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "!" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "~*" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & ":" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "=" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "`" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "\" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "]" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "[" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "}" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "{" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "´" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "~?" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & ")" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "(" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "/" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "&" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "%" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "$" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "§" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "~~" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "“" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "^" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "°" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "<" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "#" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & "'" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & "," & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & "IF(NOT(ISERROR(SEARCH(" & """" & ">" & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "special characters are not allowed" & """" & "," & _ 
"IF(NOT(ISERROR(SEARCH(" & """" & " " & """" & ",'Step 2 - Add Contact Informatio'!A4)))," & """" & "spaces are not allowed" & """" & "," & _ 
"IF(ISERROR(D8)," & """" & "error" & """" & "," & _ 
"IF(D8=FALSE," & """" & "error" & """" & "," & """" & "ok" & """" & ")))))))))))))))))))))))))))))))))" 

End Sub 

编辑:我意识到?*~在他们面前需要~否则会被渲染成各种通配符和给出误报,加上我意识到所有的搜索也应该有NOT。现在应该是好的。