在vba中声明一个excel公式作为一个常量
我在这里得到了一些帮助,在vba中声明一个公式为常数。 我有大约十个公式,都是一个主题的所有类似的变化。 我已经设置了9个,他们工作正常,下面的一个抛出一个运行时错误(运行时错误'1004';应用程序定义或对象定义的错误。
Option Explicit Public Const csFORMULA = "=CONCATENATE(""AVABIS"",IF(I2=0,"""",CONCATENATE(UPPER(AlphaNumericOnly(LEFT(I2,3))),UPPER(AlphaNumericOnly(RIGHT(I2,3))))),IF(O2=0,"""",UPPER(AlphaNumericOnly(SUBSTITUTE(O2,""0"","""")))),IF(R2=0,"""",UPPER(AlphaNumericOnly(SUBSTITUTE(R2,""0"","""")))),IF(W2=0,"""",UPPER(AlphaNumericOnly(SUBSTITUTE(W2,""0"","")))),IF(AB2=0,"""",AlphaNumericOnly(SUBSTITUTE(AB2,""0"",""""))),IF(AC2=0,"""",AlphaNumericOnly(SUBSTITUTE(AC2,""0"",""""))),IF(AD2=0,"""",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AD2,""-"",""X""),""."",""Y""),""0"",""Z"")),IF(AF2=0,"""",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AF2,""-"",""X""),""."",""Y""),""0"",""Z"")),IF(AH2=0,"""",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AH2,""-"",""X""),""."",""Y""),""0"",""Z""))))" Sub AvivaBIS() Dim lr As Long Dim cl As Range Dim rng As Range Dim mssg As String Dim ws As Worksheet Application.ScreenUpdating = False Set ws = ActiveSheet With ws lr = .Range("I" & Rows.Count).End(xlUp).Row 'add the formula to generate the unique key Range("B2:B" & lr).Formula = csFORMULA Range("B2:B" & lr) = Range("B2:B" & lr).Value End With Set rng = Nothing Set ws = Nothing Application.ScreenUpdating = True End Sub
我已经从上面删除了其他非必要的代码来简化我的问题。
我采取的步骤迄今为止:
试图直接粘贴原来的公式 – 这是有效的。
试图将声明的公式replace为简单的(例如A1 + A2) – 这是有效的。
双检查引号,看看我有正确的号码 – 这些似乎很好。
复制另一个非常相似的公式,我创build的其他人 – 这也适用。
任何build议,我可以看看解决这个问题,将受到感谢。 抛出运行时错误时,有没有办法获得更多的信息?
一如既往地感谢您抽出宝贵的时间。
你错过了一组空string双引号(就在公式中间的W2之后),并在最后有一个额外的右括号。
Public Const csFORMULA = "=CONCATENATE(""AVABIS"",IF(I2=0,"""",CONCATENATE(UPPER(AlphaNumericOnly(LEFT(I2,3))),UPPER(AlphaNumericOnly(RIGHT(I2,3))))),IF(O2=0,"""",UPPER(AlphaNumericOnly(SUBSTITUTE(O2,""0"","""")))),IF(R2=0,"""",UPPER(AlphaNumericOnly(SUBSTITUTE(R2,""0"","""")))),IF(W2=0,"""",UPPER(AlphaNumericOnly(SUBSTITUTE(W2,""0"","""")))),IF(AB2=0,"""",AlphaNumericOnly(SUBSTITUTE(AB2,""0"",""""))),IF(AC2=0,"""",AlphaNumericOnly(SUBSTITUTE(AC2,""0"",""""))),IF(AD2=0,"""",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AD2,""-"",""X""),""."",""Y""),""0"",""Z"")),IF(AF2=0,"""",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AF2,""-"",""X""),""."",""Y""),""0"",""Z"")),IF(AH2=0,"""",SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(AH2,""-"",""X""),""."",""Y""),""0"",""Z"")))"