How can I add drop-down list in excel file using matlab

28 views (last 30 days)
I use actxserver to create excel files using matlab. But I can not find a way to create cells with drop-down list(or any way to restrict the input values). Any one can help me?
Thanks a lot!
  1 Comment
Yan
Yan on 25 Feb 2012
Great Answer!
I have solved my problem , thanks a lot.
Actually, I have used :
ex.Selection.Validation.Add('xlValidateList',1,1,'=A1:A7')
to have more than one enum values.

Sign in to comment.

Accepted Answer

Friedrich
Friedrich on 24 Feb 2012
Hi,
Create a Macro, look at the source code, look at the Microsoft documention, implement it in MATLAB:
VBA:
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:="1;2;3;4"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Microsoft:
Combine these and you get the MATLAB part:
ex = actxserver('excel.application');
ex.visible = 1
wb = ex.workbooks.Add
ex.Range('A1').Select
ex.Selection.Validation.Delete;
ex.Selection.Validation.Add('xlValidateList',1,1,'1;2;3;4')
Important: First delete the Validation, otherwise you get an error if there is already a Validation created. And even more important, you have to work with the enum values for the second and third argument. With the enum strings it doesn't work. (I guess they are ordered like in the Microsoft link above, so simply count to get the correct number^^)
  2 Comments
vcmorini
vcmorini on 29 Mar 2017
Hello! This is a little difficult for me!
Why you need to look at VBA and Microsoft documentation and merge it? Where can I find information to study?
Another question, what is these two ones in bold? What do they do?
ex.Selection.Validation.Add('xlValidateList', 1,1,'1;2;3;4')
Thanks!
:)
Image Analyst
Image Analyst on 29 Mar 2017
You can talk to Excel directly with ActiveX. See attached demo.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 Feb 2012
That's getting a bit advanced. You might try looking over the MSOffice developers resources on their web site, or ask in a newsgroup devoted to Excel programming. It actually has little to do with MATLAB and is pretty much all Excel programming.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!