[英]How to Move a NamedRange in Excel - VSTO not VBA
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 我已经在网上搜索,根据文档,似乎没有移动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. NamedRange是我正在复制和粘贴的单元格,它确实将单元格值向下移动了几行,但因为它的复制它将数据留在上面的单元格中,而Delete方法会导致异常。 However the real problem is after I move the cells the NamedRange I created:
然而,真正的问题是在我移动了我创建的NamedRange的单元格之后:
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? 如何以编程方式在C#VSTO 4.0中移动NamedRange?
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. 理想情况下,在将它们放入Range之前我不必移动单元格,但如果这是唯一的解决方案,那么我将不得不继续使用它。
EDIT: 编辑:
After reading Doug Glancy's comment about trying the VBA like syntax in VSTO C# I came up with the following: 在阅读了Doug Glancy关于在VSTO C#中尝试使用类似VBA语法的评论后,我想出了以下内容:
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?!?! 但是,当我运行此代码并更改NamedRange RefersTo值时,结果是NamedRange从Excel NamedRange DropDownList中丢失了?!?!
You can "move" a named range by adding it again with a different address. 您可以通过使用不同的地址再次添加命名范围来“移动”它。 For example, in VBA:
例如,在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! 编辑 - C很难! But I managed to cobble this together in VS 2010 C#.
但我设法在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:
我不认为我需要VS 2010中的所有Type.Missing,但我很确定我已经读过早期版本需要它们:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.