Question

I've trawled the web and according to the documentation there doesn't appear to be a method to move a NamedRange: http://msdn.microsoft.com/en-us/library/microsoft.office.tools.excel.namedrange_methods(v=vs.80).aspx

I have the following code that copies cell data down a couple of rows:

activeSheet.Range[leftColumn + startRow, rightColumn + endRow].Copy();
//activeSheet.Range[leftColumn + startRow, rightColumn + endRow].Delete();
Range newRange = activeSheet.get_Range(leftColumn + (startRow + RowsToMoveDown.Count), rightColumn + (endRow + RowsToMoveDown.Count));
newRange.PasteSpecial(XlPasteType.xlPasteAll, XlPasteSpecialOperation.xlPasteSpecialOperationNone, Missing.Value, Missing.Value);

The NamedRange is the cells I'm copying and pasting, it does move the cell values down a couple of rows but because its a Copy it leaves the data in the above cells and the Delete method causes an Exception. However the real problem is after I move the cells the NamedRange I created:

rnArea = activeSheet.Range[leftColumn + startRow , rightColumn + (MyData.Values.Length + startRow)];
Name name = activeBook.Names.Add(uniqueName, rnArea);

Still refers to the original Cells Range (the location before I the moved the cells down).

How can I programmatically move a NamedRange in C# VSTO 4.0?

Ideally I wont have to move the cells before I put them in a Range but if this is the only solution, then I'll have to go with it.

EDIT:

After reading Doug Glancy's comment about trying the VBA like syntax in VSTO C# I came up with the following:

for (int i = 0; i < activeWorkbook.Names.Count; i++)
{
name = activeWorkbook.Names.Item(i + 1);
Debug.Write(name.Name.ToString());

System.Diagnostics.Debug.Write(name.RefersTo.ToString() + Environment.NewLine);
//prints out "Sheet1!$A$1:$A$25"
name.RefersTo = "Sheet1!$A$2:$A$26";
System.Diagnostics.Debug.Write(name.RefersTo.ToString());
//prints out "Sheet1!$A$2:$A$26"
}

But when I run this code and change the NamedRange RefersTo value, the result is that the NamedRange goes missing from the Excel NamedRange DropDownList?!?!

Was it helpful?

Solution

You can "move" a named range by adding it again with a different address. For example, in VBA:

Sub MoveNamedRange()
ActiveSheet.Names.Add Name:="test", RefersTo:="=$A$1"
Debug.Print ActiveSheet.Range("test").Address
ActiveSheet.Names.Add Name:="test", RefersTo:="=$A$2"
Debug.Print ActiveSheet.Range("test").Address
End Sub

This compiles and runs and yields the following in the immediate window:

$A$1
$A$2

EDIT - C is hard! But I managed to cobble this together in VS 2010 C#. It's from a Workbook project, but would also work in an addin I believe. I don't think I needed all the Type.Missing's in VS 2010, but I'm pretty sure I've read they are needed in earlier versions:

private void Sheet1_Startup(object sender, System.EventArgs e)
        {
            Globals.Sheet1.Names.Add("test", Globals.Sheet1.Range["A1"], System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
            MessageBox.Show(Globals.Sheet1.Range["test"].Address);
            Globals.Sheet1.Names.Add("test", Globals.Sheet1.Range["A2"], System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing, System.Type.Missing);
            MessageBox.Show(Globals.Sheet1.Range["test"].Address);
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top