Click here to Skip to main content
15,876,639 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I want to merge a range of 3 cells for each row where the first cell and the last cell are set to "", however it does not seem to work for me.

What I have tried:

I tried doing it for all the rows whether the first and third cell are blank or not and it works.

for(int i = 2; i <= 20;i++)
                            string c = worksheet.Cells[i, 5];
                            worksheet.Range[worksheet.Cells[i , 4], worksheet.Cells[i , 6]].Merge(1);
                            /* currRange.Value2 = worksheet.Cells[i, 5];

Can you please help out?
Updated 6-Jul-23 0:48am

You have already posted this question at Merge cells from different columns in a datagridview C#[^], and been given a number of suggestions. If you have additional informationor questions then please use the Improve question link below the original, and add complete details there. Please do not repost the same question.
Share this answer
LiterallyGutsFromBerserk 23-Jun-23 11:40am    
Sorry for the inconvenience. I'll improve my questions from now on instead of posting new threads
Recommended method of doing work from C# was given here: Export datagridview to excel while keeping the formatting[^]:
1. Create a Macro recording of the actions that you wish to perform
2. View the VBA code generated for you
3. Use the VBA code to guide you for writing your C# code

If you follow these steps, and the original answer given, you will easily solve this question.
Share this answer
LiterallyGutsFromBerserk 23-Jun-23 11:41am    
You're absolutely right! Thanks again and sorry for the disturbance...
Graeme_Grant 23-Jun-23 20:44pm    
It is not a disturbance. Let the macro recorder do all the hard work for you. When recording Excel macros, think about if you require relative or absolute cell reference. By default it is set to absolute. the option is right next to the record macro button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900