2017-10-06 110 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。現在應該是好的。