Home » excel » c# – Range.Replace in Excel

c# – Range.Replace in Excel

Posted by: admin May 14, 2020 Leave a comment

Questions:

I try to replace some cells with Range.Replace, but when I try to do it, my program only change the first cell find it.

How I can do ?

range = sheet.UsedRange;
// leer las celdas
int rows = range.Rows.Count;
int cols = range.Columns.Count;

Excel.Range startCell = sheet.Cells[1, 1];
Excel.Range endCell = sheet.Cells[rows, cols];
sheet.Range[startCell, endCell].Replace(@"'C:\", @"'C:\Gestion\");

Now I try to do this:

// seleccion rango activo
range = sheet.UsedRange;
// leer las celdas
int rows = range.Rows.Count;
int cols = range.Columns.Count;

Excel.Range startCell = sheet.Cells[1, 1];
Excel.Range endCell = sheet.Cells[rows, cols];
sheet.Range[startCell, endCell].Replace(@"C:\", @"C:\Gestion\", Excel.XlLookAt.xlPart, Excel.XlSearchOrder.xlByColumns, false, Type.Missing, false, false); 

But I have the same problem, only change one cell in my sheet :S

How to&Answers:

Your code is correct. Here are some things you could check:

(1) Are your search settings set to be case sensitive? If this is the problem, try replacing your code with this:

sheet.Range[startCell, endCell].Replace(@"'C:\", @"'C:\Gestion\", matchcase: false);

(2) You use the apostrophe (‘) in your search string. There are multibyte variants of this character – does your spreadsheet use these instead of actual apostrophes? (An apostrophe is character 39 in ASCII, or 27 in hexadecimal.)

(3) The apostrophe has a special meaning in Excel when used as the first character in a cell – it tells Excel that the cell contains text data. (Try entering '1 in a cell instead of 1 to see the difference.) If you want a cell to start with an actual apostrophe, you’ll need to double it up – so ''C:\ will be rendered as 'C:\ in the cell, and this is what will be searched (ignoring the first apostrophe).

My guess is that (3) is the most likely, but if none of the above suggestions work then it would help if you could provide some more information.

Answer:

I made this one to remove ‘ and it’s working good:

            var excelApp = new Microsoft.Office.Interop.Excel.Application();
            Workbook wb = excelApp.Workbooks.Open(@"c:\folder\excelfile.xlsx", Type.Missing, Type.Missing,
                                                   Type.Missing, Type.Missing,
                                                   Type.Missing, Type.Missing,
                                                   Type.Missing, Type.Missing,
                                                   Type.Missing, Type.Missing,
                                                   Type.Missing, Type.Missing,
                                                   Type.Missing, Type.Missing);
            excelApp.DisplayAlerts = false;
            var ws = wb.Worksheets;
            var worksheet = (Worksheet)ws.get_Item("sheetname");

            Range range = worksheet.UsedRange;

            int rows = range.Rows.Count;
            int cols = range.Columns.Count;

            Range startCell = worksheet.Cells[1, 1];
            Range endCell = worksheet.Cells[rows, cols];

            worksheet.Range[startCell, endCell].Replace("'", "", MatchCase: false);


            wb.Close(true);
            excelApp.Quit();