Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excel Validation Drop Down list using VBA

Tags:

excel

vba

I have an array of values. I want to show those values in Excel Cell as drop down list using VBA.

Here is my code. It shows "Type Mismatch Error!"

Dim xlValidateList(6) As Integer
xlValidateList(1) = 1
xlValidateList(2) = 2
xlValidateList(3) = 3
xlValidateList(4) = 4
xlValidateList(5) = 5
xlValidateList(6) = 6

With Range("A1").Validation
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=ValidationList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With

The problem occurs in following line...

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _

Please let me where the problem is... Thanks in advance

like image 257
logan Avatar asked Sep 19 '13 02:09

logan


People also ask

How do I add a Data Validation List in Excel VBA?

Use the Add method of the DataValidation property of the Range object. Use the AlertStyle parameter to specify the style of the alert warning dialog. Use the Operator parameter together with the Formula parameters to specify a rule.

Which VBA control works as a drop down list?

Create drop down lists in a UserForm, by using the ComboBox control. In this example, there are two ComboBox controls, one for Part ID, and one for Location.


1 Answers

You are defining your array as xlValidateList(), so when you try to assign the type, it gets confused as to what you are trying to assign to the type.

Instead, try this:

Dim MyList(5) As String
MyList(0) = 1
MyList(1) = 2
MyList(2) = 3
MyList(3) = 4
MyList(4) = 5
MyList(5) = 6

With Range("A1").Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
         Operator:=xlBetween, Formula1:=Join(MyList, ",")
End With
like image 162
SeanC Avatar answered Sep 22 '22 17:09

SeanC