简体   繁体   English

在Excel中从范围中删除单元格

[英]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.

相关问题 将Excel表格中的一系列单元格导入数据表 - Import a range of cells from excel sheet into a datatable 将值添加到<list>从 excel 细胞范围</list> - Adding a value to <List> from range of excel cells 使用Office Interop从Excel Range(单元格)读取RTF文本 - Reading Rich Text from Excel Range (cells) with Office Interop 如何将不连续范围的单元格从 Excel 传递到 ExcelDNA 函数 - How to pass discontinuous range of cells from Excel to ExcelDNA function 将特定范围的 Excel 单元格从一个工作表复制到另一个工作表 - copying of specific range of excel cells from one worksheet to another worksheet 使用 VSTO 将一系列单元格从 excel 复制到 powerpoint - Copying a range of cells from excel into powerpoint using VSTO C#Interop从给定范围获取Excel单元格背景 - C# Interop get Excel cells background from given range 从特定关键字下方复制特定范围的excel单元格 - Copying specific range of excel cells from below a specific keyword 从Excel文件C#中查找和删除usedRange中的空单元格 - Find and remove empty cells in a usedRange from an excel file C# 如何使用Excel.Interop将列表中的值填充到Excel中的一系列单元格? - How to populate values from a list to a range of cells in an Excel using Excel.Interop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM