2016-01-29 48 views
0

首先,這是一個長而醜陋的公式警告。長公式在放入單元格時起作用,但在插入VBA時不起作用

我想要做的是讓VBA將此公式插入到Excel中。如果我得到VBA插入它沒有'=',然後手動將其添加到Excel中,它證明了公式的作品。但是,如果我嘗試讓VBA插入並運行公式,並帶有'='前綴,那麼所有內容都將一起運行,然後出現運行時錯誤。

任何想法如何讓VBA插入並運行此公式?

ActiveCell.FormulaR1C1 = _ 
"=IF(P1=""EQS FW"",IF(Statistics!B3=""Cadmium"",IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$5,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$5),'Hardness-bands'!$D$5,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$6,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$6),'Hardness-bands'!$D$6,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$7,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$7),'Hardness-bands'!$D$7,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$8,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))" & _ 
"<'Hardness-bands'!$C$8),'Hardness-bands'!$D$8,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$9,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$9),'Hardness-bands'!$D$9,""Err""))))),IF(Statistics!B4=""Copper"",IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$13,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$13),'Hardness-bands'!$D$13,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$14,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$14),'Hardness-bands'!$D$14,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$15,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300," & _ 
"MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$15),'Hardness-bands'!$D$15,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$16,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$16),'Hardness-bands'!$D$16,""Err"")))),IF(Statistics!B4=""Zinc"",IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$20,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$20),'Hardness-bands'!$D$20,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$21,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$21),'Hardness-bands'!$D$21,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-ban" & _ 
"ds'!$B$22,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$22),'Hardness-bands'!$D$22,IF(AND(INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))>'Hardness-bands'!$B$23,INDIRECT(ADDRESS(ROW(INDEX($B$3:$B$300,MATCH(""Alkalinity as CaCO3"",$B$3:$B$300,0))),9))<'Hardness-bands'!$C$23),'Hardness-bands'!$D$23,""Err"")))),Statistics!R4))),Statistics!R3)" 
+0

事實證明,這個問題與'ActiveCell.FormulaR1C1'有關 - 因爲我的公式沒有輸入R1C1符號! –

回答

0

哇,這是相當大的。你絕對應該把它打破一個相當的數額。

無論如何,我認爲這可能是因爲你試圖在你的字符串中插入語音標記。在VBA調試模式下,複製整個字符串並在即時窗口中查看它。它是否與Excel單元格中的字符串完全相同?我的猜測不會。

嘗試更換雙語音標記: P1 = 「」 EQS FW 「」

像這樣的東西: P1 =」 & 「」 「」 & 「EQS FW」 & 「」 「」

這將插入演講正確標誌着你。

另一種認爲,這將是棘手的做演講標記的調整,所以試圖通過它在廣闊的公式的一小部分,並建立它逐漸開始。