【问题标题】:How to extend a formula in VBA past 1 line of code如何将 VBA 中的公式扩展到超过 1 行代码
【发布时间】:2013-08-01 12:59:39
【问题描述】:

所以我没有太多的编程经验,几乎没有 VBA。我的主要问题是我的代码中的公式超出了 1 行,当我包含下划线、空格然后开始新行时,我收到一个错误。

我附上了代码的图片,可能有不必要的代码行,因为我录制了一个宏来获取代码。

有关我正在尝试做的事情的更多信息:

我有一个包含在使用“数据验证”的单元格中的列表,并且根据从该列表中的选择,下面的单元格将输出某个列表。

这些列表的信息存储在工作簿的其他工作表中。

我能够在“数据验证”列表源框中开发一个适用于多个输入的 IF 语句。但是,我有 84 种可能性,我无法将所有单独的 if 语句都放在列表源框中。因此我决定尝试通过记录几个输入“数据验证”if语句的宏来使用VBA手动输入公式。

代码如下:

Sub HelpSetUp()
     With Selection.Validation
         .Delete
         .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
         xlBetween, Formula1:= _
         "=if($B$2='fuel columns'!$A$1,agriculturalbiproduct,if($B$2='fuel      columns'!$B$1,agriculturalresidue,if($B$2='fuel columns'!$C$1,agriculturalwaste,Nofuel)))"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With
End Sub 

【问题讨论】:

    标签: excel if-statement extending vba


    【解决方案1】:

    当您有很长的文本要挤入时,您需要使用“&”和_将其分成块。

    这样

    dim aString 作为字符串

    aString = "four score and seven years ago our fathers " & _ 
        "set forth on this continent a new nation, " & _
        "conceived in liberty and dedicated to the " & _
        "proposition that all men are created equal."
    

    请务必在 & 和 _ 之间留一个空格。

    SMW

    【讨论】:

    • 如果您接受了我的回答,请点击大号下方的复选标记。这将是我第一个接受的答案!
    猜你喜欢
    • 1970-01-01
    • 2023-03-05
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多