[英]Excel VBA 1004 Error formulas
我已經在 VBA 工作了大約一個月,我覺得我對這門語言有很好的掌握。 不幸的是,這就是這個錯誤如此令人沮喪的原因。
我正在嘗試為單元格分配一個公式,但我沒有使用英式分隔符(所以是逗號,而不是分號)。 我仍然收到運行時錯誤。
實際公式的語法很好,因為我已經嘗試在 Excel 中使用它,並且它可以滿足我的需要,所以我必須假設錯誤出在我的 VBA 語法中,如下所示:
With DecileChooser
Range("A1").CurrentRegion.PasteSpecial
Range("I1").Value = CurrentPD
Range("I2").Value = CompanyDomain
DecileChooser.Range("J2").Formula = "=IF($I$1 > D2, G2, "")"
Range("J2:J11").FillDown
Range("K1").FormulaLocal = "=max(J1:J11)"
Range("K1").Value = DecileValue
End With
這不是整個子程序,只是操作部分。
這里的問題是代碼正在讀取公式中的雙引號作為公式的結尾,即它讀取的是:
DecileChooser.Range("J2").Formula = "=IF($I$1 > D2, G2, "
但是剩下的“)”存在語法問題
您必須使用雙引號來表示您沒有結束字符串。
With DecileChooser
Range("A1").CurrentRegion.PasteSpecial
Range("I1").Value = CurrentPD
Range("I2").Value = CompanyDomain
DecileChooser.Range("J2").Formula = "=IF($I$1 > D2, G2, """")"
Range("J2:J11").FillDown
Range("K1").FormulaLocal = "=max(J1:J11)"
Range("K1").Value = DecileValue
End With
在雙引號上加倍:
DecileChooser.Range("J2").Formula = "=IF($I$1 > D2, G2, """")"
或者
dq = Chr(34)
DecileChooser.Range("J2").Formula = "=IF($I$1 > D2, G2," & dq & dq & ")"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.