Search code examples
excelmatlabactivexexcel-automation

How can I turn on Data/Filter mode on Excel sheets from MATLABvia ActiveX?


My MATLAB code produces data that I want to send to Excel. I use ActiveX Server in MATLAB to both produce the Excel file and to do the export (I can control more properties of the resulting Excel file this way than by using the 'writetable' command). In the produced Excel file, I manually have to chose the menu 'Data/Filter' to turn on the function letting me filter and sort each column (visible as the small triangles in the column headings as shown in the illustration below).

How can I turn on this function with ActiveX from MATLAB?

Excel screenshot showing sort/filter dropdowns


Solution

  • Call the AutoFilter method on an Excel Range object. For example, if you have a worksheet object MySheet:

    rangeToFilter = get(MySheet,'Range','A1:B3');
    rangeToFilter.AutoFilter