Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

excel - When i add this code in workbook received an error

I have below code which is create a data validation for a range but whenever i add this code in the workbook. Received this error "we found a problem with some content" while opening the workbook but when i remove this from workbook then it works fine.

and when i click on save the updated data in workbook it gives the option to select a folder to save the file.

enter image description here

Can someone please look into this that what is the issue with the code.

Dim list1               As Range
    Application.EnableEvents = False
    Dim validationFormula   As String

    Set list1 = Sheet3.Range("A1:A100")

    Dim myCell As Range
    For Each myCell In list1
        If Not IsEmpty(myCell) Then
            validationFormula = validationFormula & myCell.Value2 & ","
        End If
    Next

    validationFormula = validationFormula & Chr(160)

    With Sheet1.Range("C4:C2000").Validation
        .Delete
        .Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=validationFormula
        .IgnoreBlank = False
        .InCellDropdown = True
end with
question from:https://stackoverflow.com/questions/65907038/when-i-add-this-code-in-workbook-received-an-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try the next way, please:

   Dim list1 As Range
    Application.EnableEvents = False
    Dim validationFormula As String

    Set list1 = Sheet3.Range("A1:A100")
    validationFormula = Join(Application.Transpose(Application.index(list1, 0, 1)), ",")

    With Sheet1.Range("C4:C2000").Validation
        .Delete
        .Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=validationFormula
        .IgnoreBlank = False
        .InCellDropdown = True
   End With
   Application.EnableEvents = True
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...