How to sort in Excel using MATLAB?

6 views (last 30 days)
Keith
Keith on 5 May 2014
Commented: Image Analyst on 6 May 2014
I am trying to select a row turn on auto-filter and then do an ascending sort for one column of data. I have recorded a macro from Excel:
Sub Macro1()
'
' Macro1 Macro
'
'
Range("A1:B1").Select
Selection.AutoFilter
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort.SortFields.Add Key:=Range _
("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").AutoFilter.Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
I have figured out how to turn on the filter. I'm having problems now activating the sort by ascending order.
If anyone has any insight into this that would be great. Thank you.

Answers (1)

Image Analyst
Image Analyst on 5 May 2014
Are you wanting to do the same commands from MATLAB on the Excel worksheet by controlling Excel from MATLAB via ActiveX commands? Is that what you're asking? Or do you want Excel to call a sorting engine that you built in MATLAB? Exactly who will be doing the sorting? And who will be directing/calling the sort engine?
  2 Comments
Keith
Keith on 5 May 2014
I would like to do the same commands in MATLAB on Excel by controlling Excel from MATLAB via ActiveX commands. Excel would be doing the sorting. MATLAB would call all of the commands with ActiveX.
Image Analyst
Image Analyst on 6 May 2014
See my ActiveX demo. Of course I didn't do sorting, so you'll have to use the commands you see in your macro and put those in the MATLAB code. That's what's nice about Excel - you can record a macro to see the commands you should use rather than trying to guess from 50 bazillion possible ActiveX methods. You may need to adapt those methods slightly since MATLAB is slightly different than VBA, but the names of the methods should be the same. And of course you'll have to modify my demo to skip stuff you don't want to do. Good luck and let me know how it goes. If it doesn't work, attach your workbook and m-file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!