Reputation: 455
I've created a custom Excel Ribbon which contains two buttons. btnFillColor and btnUndo.
When the user clicks on btnFillColor button, the selected cells are filled with some color. 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.
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.
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;
}
Upvotes: 2
Views: 1331
Reputation: 455
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;
}
Upvotes: 1