[英]Remove cells from range in Excel
I've created a custom Excel Ribbon which contains two buttons. 我创建了一个包含两个按钮的自定义Excel功能区。 btnFillColor and btnUndo. btnFillColor和btnUndo。
When the user clicks on btnFillColor button, the selected cells are filled with some color. 当用户单击btnFillColor按钮时,所选单元格将填充某种颜色。 At the same time a Inputbox is displayed which asks the user to give a name to the selected range of cells. 同时,将显示一个输入框,要求用户为选定的单元格区域命名。
When the user clicks on btnUndo the cells are cleared and the name of the corresponding range is deleted in exact reverse order. 当用户单击btnUndo时,将清除单元格,并以完全相反的顺序删除相应范围的名称。
When the user clicks on btnFillColor and gives a name to the selected cells and if the same name already exists the range is appended to the existing range under the same name. 当用户单击btnFillColor并为选定的单元格命名时,如果已经存在相同的名称,则该范围将以相同的名称附加到现有范围。
Now my problem is: When the user clicks on undo button after two different ranges are added under same name then only those cells should be cleared and removed out of range which were added last to the range. 现在我的问题是:当用户以相同的名称添加了两个不同的范围后,单击“撤消”按钮时,只有那些单元格应该被清除并移出最后添加到该范围的范围。 The other one should remain intact alongwith the name given earlier. 另一个应与先前给出的名称保持不变。 How to achieve this? 如何实现呢?
Here is the code. 这是代码。
private void btnFillColor_Click(object sender, RibbonControlEventArgs e)
{
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
string strFilledRangeAddress = Globals.ThisAddIn.Application.Selection.Address.ToString();
Excel.Range FilledRange = ws.get_Range(strFilledRangeAddress);
FilledRange.Interior.Color = Color.YellowGreen;
string tagName = Microsoft.VisualBasic.Interaction.InputBox("Enter tag name:");
if (lstFilledRangeName.Contains(tagName))
{
Excel.Range ExistingRange = ws.get_Range(tagName);
FilledRange = Globals.ThisAddIn.Application.Union(ExistingRange, FilledRange);
}
else
{
lstFilledRangeName.Add(tagName);
}
FilledRange.Name = tagName;
lstFilledRangeAddresses.Add(strFilledRangeAddress);
if (btnUndo.Enabled == false)
btnUndo.Enabled = true;
}
private void btnUndo_Click(object sender, RibbonControlEventArgs e)
{
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
Excel.Range FilledRange = ws.get_Range(lstFilledRangeAddresses[lstFilledRangeAddresses.Count - 1]);
FilledRange.Interior.ColorIndex = 0;
lstFilledRangeAddresses.RemoveAt(lstFilledRangeAddresses.Count - 1);
if (lstFilledRangeAddresses.Count == 0)
btnUndo.Enabled = false;
}
To clear only the last filled cells and remove them from the existing range I am keeping a track of what selection user made in a datatable. 为了仅清除最后填充的单元格并将其从现有范围中删除,我一直在跟踪用户在数据表中所做的选择。 When the user clicks on undo button, before clearing the cells and name I am checking whether the user made a selection under the same name previously. 当用户单击撤消按钮时,在清除单元格和名称之前,我正在检查用户以前是否使用相同名称进行选择。 If yes, then first I clear all the cells under that name and also remove the name. 如果是,则首先清除该名称下的所有单元格,然后也删除该名称。 After that again I fill the previous selection with the color and give it the same name. 之后,我再次用颜色填充先前的选择,并为其赋予相同的名称。
Here is the modified code: 这是修改后的代码:
DataTable dtRangeName_Address = new DataTable();
private void RnD_Ribbon_Load(object sender, RibbonUIEventArgs e)
{
btnUndo.Enabled = false;
dtRangeName_Address.Columns.Add("Name");
dtRangeName_Address.Columns.Add("Address");
}
private void btnFillColor_Click(object sender, RibbonControlEventArgs e)
{
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
string strFilledRangeAddress = Globals.ThisAddIn.Application.Selection.Address.ToString();
Excel.Range FilledRange = ws.get_Range(strFilledRangeAddress);
FilledRange.Interior.Color = Color.YellowGreen;
DataRow drRangeName_Address = dtRangeName_Address.NewRow();
bool TagAlreadyExists = false;
string tagName = Microsoft.VisualBasic.Interaction.InputBox("Enter tag name:");
for (int i = dtRangeName_Address.Rows.Count - 1; i >= 0; i--)
{
if (dtRangeName_Address.Rows[i][0].ToString() == tagName)
{
Excel.Range ExistingRange = ws.get_Range(dtRangeName_Address.Rows[i][1].ToString());
FilledRange = Globals.ThisAddIn.Application.Union(ExistingRange, FilledRange);
drRangeName_Address[1] = FilledRange.Address.ToString();
drRangeName_Address[0] = tagName;
TagAlreadyExists = true;
break;
}
}
if (TagAlreadyExists == false)
{
drRangeName_Address[1] = strFilledRangeAddress;
drRangeName_Address[0] = tagName;
}
FilledRange.Name = tagName;
dtRangeName_Address.Rows.Add(drRangeName_Address);
if (btnUndo.Enabled == false)
btnUndo.Enabled = true;
}
private void btnUndo_Click(object sender, RibbonControlEventArgs e)
{
Excel.Worksheet ws = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet;
Excel.Range FilledRange = ws.get_Range(dtRangeName_Address.Rows[dtRangeName_Address.Rows.Count - 1][1].ToString());
string tagName = dtRangeName_Address.Rows[dtRangeName_Address.Rows.Count - 1][0].ToString();
Excel.Range ExistingRange = null;
bool TagAlreadyExists = false;
for (int i = dtRangeName_Address.Rows.Count - 2; i >= 0; i--)
{
if (dtRangeName_Address.Rows[i][0].ToString() == tagName)
{
ExistingRange = ws.get_Range(dtRangeName_Address.Rows[i][1].ToString());
TagAlreadyExists = true;
break;
}
}
FilledRange.Interior.ColorIndex = 0;
FilledRange.Name.Delete();
if (TagAlreadyExists == true && ExistingRange != null)
{
ExistingRange.Interior.Color = Color.YellowGreen;
ExistingRange.Name = tagName;
}
dtRangeName_Address.Rows.RemoveAt(dtRangeName_Address.Rows.Count - 1);
if (dtRangeName_Address.Rows.Count == 0)
btnUndo.Enabled = false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.