How to extend a formula in VBA past 1 line of code

两盒软妹~` 提交于 2019-12-08 11:50:32

问题


So I do not have too much experience with programming and almost non with VBA. My main issue is that the formula in my code extends past 1 line and when I include an underscore, space and then start a new line, I receive an error.

I have attached a pic of the code, there is probably unnecessary lines of code because I recorded a macro to get the code.

More information on what I am trying to do:

I have a list contained in a cell using "data validation" and based on the selection from that list, the cell below will output a certain list.

The information for these lists are stored on other worksheets in the workbook.

I was able to develop an IF statement in the "data validation" list source box that worked for several inputs. However I have 84 possibilities and i could not fit all the individual if statements in the list source box. Therefore I decided to try and manually input the formula using VBA by recording a macro of the several input "data validation" if statement.

Here is the code:

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 

回答1:


When you have a long bit of text to squeeze in, you need to break it up into chunks using "&" and _.

Like this

dim aString as string

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."

Be sure to leave a space between the & and the _.

SMW



来源:https://stackoverflow.com/questions/17979916/how-to-extend-a-formula-in-vba-past-1-line-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!