Find and Replace a specific String from excel work book

3 visualizaciones (últimos 30 días)
Akash Rana
Akash Rana el 18 de Dic. de 2019
Respondida: Rahul el 19 de Feb. de 2025
I have to find and replace a specific String from an excel work book which contains n number of sheets. The string is available in random location in different sheet's.Example I want to replace the string Rana_1 from all sheets with Akash_1 .One more thing format of the work book should not change. I tried regexp but it is not usefull .

Respuestas (1)

Rahul
Rahul el 19 de Feb. de 2025
I understand that you wish to replace centrain cells with a particular text with a different text. To achieve this, consider using:
  • This can be done by looping through each cell of the range of the excel sheet and checking as well as replacing the text in the cell with required text.
  • Another approach can be to read the data from the excel file using 'readcell' function and the apply the tranformation to all the cells using 'cellfun' with a custom helper function to replace the text. Then 'writecell' can be used to write the modified data back to the excel sheet. Here is an example:
FileName = 'yourFile.xlsx';
oldString = 'Rana_1';
newString = 'Akash_1';
% Read, modify and write the data
data = readcell(FileName);
modifiedData = cellfun(@(x) replaceIfMatch(x, oldString, newString), data, 'UniformOutput', false);
writecell(modifiedData, FileName);
% Helper function to replace matching strings
function output = replaceIfMatch(input, oldStr, newStr)
if ischar(input) && strcmp(input, oldStr)
output = newStr;
else
output = input;
end
end
Additionally, the following MATLAb Answer and File Exchange submission can be referred:
The following MathWorks documentations can be referred to know more:
Hope this helps! Thanks.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by