How do I delete an entire row from Excel from MATLAB?

48 ビュー (過去 30 日間)
James Boyle
James Boyle 2017 年 7 月 5 日
編集済み: Mark Anthony Rivas 2020 年 2 月 29 日
Ultimately I want to delete an entire row from an excel file (say, row 10) and shift the rows 11 onwards up so that there isn't an empty space in the file.
I can envisage a long winded solution to this using xlswrite to overwrite all the previous rows, but is there a simple solution to this? I can't find a way of doing it anywhere.
Many thanks!
Using R2016a
  2 件のコメント
Image Analyst
Image Analyst 2017 年 7 月 5 日
How comfortable are you using ActiveX? Are you using Windows?
James Boyle
James Boyle 2017 年 7 月 5 日
I have not used ActiveX at all, I am using windows 10.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2017 年 7 月 10 日
It's not hard to do in matlab if you know excel VBA. If you don't, it's just as easy to record a macro in excel and use that rather than trying to do it in matlab.
The following should work (untested since I don't have matlab installed on this machine):
file = 'C:\somewhere\somefile.xlsx';
sheet = 'Sheet1'; %can be name or numeric index
row = 10;
excel = actxserver('Excel.Application');
workbook = excel.Workbooks.Open(file);
worksheet = workbook.Worksheets.Item(sheet);
worksheet.Rows.Item(row).Delete;
workbook.Save;
excel.Quit;
  3 件のコメント
Sara
Sara 2018 年 1 月 31 日
If I wanted to hide the row instead of deleting it, what would change from this code above?
Mark Anthony Rivas
Mark Anthony Rivas 2020 年 2 月 29 日
編集済み: Mark Anthony Rivas 2020 年 2 月 29 日
This will only remove the items on the table but not on excel.
function UITableCellSelection(app, event)
indices = event.Indices
data = app.UITable.Data;
row = indices(:,1);
app.UITable.Data(row,:) = [];
end

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by