how to add a drop down list in excel using vbs

℡╲_俬逩灬. 提交于 2020-02-04 03:55:14

问题


Hi I am trying to add a drop down in a excel using vbs and i am getting the below error.

vbs(18, 15) Microsoft VBScript compilation error: Syntax error

I even recorded a marco and used the code from there still it didn't work.

Line 18 and col 15 is the line with the add keyword.

Range("A1").Select
    With Selection.Validation
        .Delete
        .Add (Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
        xlBetween, Formula1:="=$Q$9:$Q$11")
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
    End With

Q9:Q11 has a sample data set What am i doing wrong?


回答1:


You need to replace the vba intrinsic constants of xlValidateList with their direct vbs numeric equivalent (which you can get via VBA help). This works:

Dim objExcel, objWB, objws
Set objExcel = CreateObject("excel.application")
Set objWB = objExcel.Workbooks.Add
Set objws = objWB.Sheets(1)
With objws.Range("A1").Validation
        .Add 3, 1, 1, "=$Q$9:$Q$11"
        .IgnoreBlank = True
        .InCellDropdown = True
        .InputTitle = ""
        .ErrorTitle = ""
        .InputMessage = ""
        .ErrorMessage = ""
        .ShowInput = True
        .ShowError = True
 end With



来源:https://stackoverflow.com/questions/21788845/how-to-add-a-drop-down-list-in-excel-using-vbs

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