Search code examples
excelmatlabactivex

How to delete Multiple rows in excel file using activex in Matlab


Activesheet.Rows.Item(1).Delete;

The above line will delete first row in the active excel sheet.

I want to delete multiple rows so I used the following line, but it did not work.

Activesheet.Rows.Item([1,2,5,64]).Delete;

Solution

  • If you want a full Matlab approach you could try this out:

    rows_to_delete = [1 2 5 64];
    
    data = xlsread(file_path);
    data(rows_to_delete,:) = [];
    
    delete(file_path);
    xlswrite(file_path,data);
    

    Using Excel COM:

    Activesheet.Rows('1, 2, 5, 64').EntireRow.Delete
    

    or:

    Activesheet.Range('1, 2, 5, 64').Rows.EntireRow.Delete
    

    You could even try a more consistent notation, eventually, using 1:1, 2:2, 5:5, 64:64 instead of 1, 2, 5, 64. But I always used the latter without problems in my code.