Find and Replace a specific String from excel work book

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 .

回答 (1 件)

Rahul
Rahul 2025 年 2 月 19 日

0 投票

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.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

2019 年 12 月 18 日

回答済み:

2025 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by