Delete specific rows in excel

23 visualizaciones (últimos 30 días)
Cristian Martin
Cristian Martin el 30 de Mayo de 2022
Comentada: dpb el 30 de Mayo de 2022
Hi,
I want to delete rows from an excel file before writte in to it, starting row no 9 till end. I want this for initialize a new file but to keep the first 8 rows for labels and other stuff. The file model is attached.
I tried many ways with no succes, including the one below:
data = xlsread(test.xlsx);
data(9, :) = [];
delete(test.xlsx); % So extra rows on the end of old worksheet don't stay there after writing new data.
xlswrite(test.xlsx);
Thanks!

Respuesta aceptada

dpb
dpb el 30 de Mayo de 2022
Use read/writematrix instead
data=readmatrix('text.xlsx');
writematrix(nan(size(data)),'test.xlsx',"WriteMode",'inplace','Range','A9','UseExcel',1,"PreserveFormat",1)
There are no builtin MATLAB functions that will read/return the textbox you've got stuck in that header region so you've got to blank out the other data that is in the file to do what you want with prepackaged functions.
Be a lot simpler to just create a template workbook and use it to create the new file from...
There are user-written functions on FEX and some have been posted here that use the ActiveX interface and package functions for some low-level operations such as this, but I don't have a specific link at hand...well, let's see -- I do have a utility package I did download some time back I've fiddled with some...let's see what's in it, I forget.
Excel = actxserver('Excel.Application')
d=dir('test.xlsx')
Workbook = Excel.Workbooks.Open(fullfile(d.folder,d.name))
Excel.Range('A9:E34').Select
Excel.Selection.Clear
Excel.ActiveWorkbook.Save
Excel.ActiveWorkbook.Close(false)
delete(Excel); clear Excel
is the basics -- I just used a hardcoded range; you'd have to figure that range out dynamically if not known a priori. There's a function that will return the used range in a sheet that could be used. To use ActiveX, one has to just pore through the VBA help files and find the functions of use and figure out what Excel syntax can be used w/ ActiveX; much that works in VBA doesn't translate over owing to there not being the VBA compiler to translate syntax.
  2 comentarios
Cristian Martin
Cristian Martin el 30 de Mayo de 2022
Thanks dpb, I'll look into ActiveX and search a solution. Another solution of mine was to use a model stored in a folder, and every time I want to initiate the file to be overwritten the actual file, but I don't know how to manage the copy file, move file, because will be a GUI and the directory where is the app or partition could be changed and is a bad solution. Thanks again!
dpb
dpb el 30 de Mayo de 2022
The app has got to be able to know/find whatever file it's going to be writing to, so you'll have to have some way to determine that.
As far as the GUI, if you package an app, files that it needs are packaged with it so the template will be there.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by