简体   繁体   English

从Excel转换为VBA代码的公式

[英]Formula from Excel into VBA Code

Hello fellow programmers! 各位程序员大家好!

I am new to VBA and shall make big changes in an existing Excel template... Now my problem is, while trying to export a Formula from excel (it is supposed to work later on button click), that I am unable to use this simple step: 我是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

This does work but when inserting my sadly big formula 这确实有效,但是在插入我可悲的大公式时

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

I get an Syntax Error, I even tried escaping the "" with \\"\\" but that just worked for half of the formula. 我收到语法错误,我什至尝试用\\“ \\”转义“”,但这仅适用于公式的一半。 Does anybody know what the problem is? 有人知道问题出在哪里吗?

I tried converting it from FormulaLocal to just Formula and put it into "regular" VBA and it seems to work on my end at least. 我尝试将其从FormulaLocal转换为只是Formula然后将其放入“常规” VBA中,至少它似乎对我FormulaLocal I would have put each IF on a separate line but after 25 _ 's it gets mad. 我会把每个IF放在单独的行上,但是25 _之后会发疯。 (If I mistranslated any of the functions, let me know!) (如果我误译了任何功能,请告诉我!)

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

Edit : I realized that ? 编辑 :我意识到了? , * , and ~ needed a ~ in front of them or they would be rendered as various wildcards and give false positives, plus I realized all the searches should have NOT in them as well. *~在它们前面需要~ ,否则它们将被呈现为各种通配符并给出假肯定,再加上我意识到所有搜索也NOT应包含在其中。 Should be good now. 现在应该很好。

I really appreciate your help guys, sadly none of them worked. 非常感谢您的帮助,可惜他们都没有帮助。 But I was able to convince my boss to use a regex. 但是我能够说服老板使用正则表达式。 Thanks again, @u8it your approach did work, but I do not know how to mark it as answer 再次感谢,@ u8it您的方法确实有效,但我不知道如何将其标记为答案

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

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