Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to place a text value spanning four rows. I thought/hoped this would work:

C#
    private int curDescriptionTopRow = 8;
. . .
private void AddDescription(String desc)
    {
        int curDescriptionBottomRow = curDescriptionTopRow + 3;

        _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]].Font.Bold = true;
        _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]].VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
        _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]].Value2 = desc;

        curDescriptionTopRow = curDescriptionTopRow + 4;
    }


I need the first description to display, vertically centered, in cells A8 - A11 (column A, rows 8-11).

The code above adds the description in all four rows, rather than just one time in the four rows.

How can I prevent the three redundant appearances of the description?
Posted
Updated 4-Nov-15 8:33am
v2
Comments
Richard Deeming 4-Nov-15 14:46pm    
Are you looking for the Merge method[^]?
B. Clay Shannon 4-Nov-15 14:48pm    
I don't know; I'll czech it out - thanks for the link!

1 solution

Thanks to Richard deeming my question worthy of a hint, I was able to find out that defining and then merging the range works:

C#
private int curDescriptionTopRow = 8;
. . .
private void AddDescription(String desc)
{
    int curDescriptionBottomRow = curDescriptionTopRow + 3;

    var range =
        _xlSheet.Range[_xlSheet.Cells[curDescriptionTopRow, 1], _xlSheet.Cells[curDescriptionBottomRow, 1]];
    range.Merge();

    range.Font.Bold = true;
    range.VerticalAlignment = Microsoft.Office.Interop.Excel.XlVAlign.xlVAlignCenter;
    range.Value2 = desc;
}
 
Share this answer
 

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