简体   繁体   English

VBA在Excel上删除无颜色的单元格和相邻单元格

[英]VBA on Excel to delete cells with no color and neighbour cells

So I try to figure out a way to do the following trick using VBA: 因此,我尝试找出一种使用VBA进行以下技巧的方法:

Assuming we got a huge Excel file. 假设我们有一个巨大的Excel文件。

C500 has some text. C500有一些文字。 M500 has a value and it can either have a color fill, or just being a default white cell with a number. M500有一个值,可以填充颜色,也可以只是带有数字的默认白色单元格。 What I want to achieve, is to delete C500 AND M500 when M500 has no color fill. 我要实现的是在M500没有颜色填充时删除C500和M500。

I know it is a simple task, at least it looks like that and I know that it can probably be solved just by a few lines of code. 我知道这是一个简单的任务,至少看起来像这样,而且我知道只需几行代码就可以解决。 I still can't find what I need on google or stack overflow by searching for it, probably because of my poor searching skills or because what I wanna do is very specific. 我仍然无法通过搜索来在Google上找到我所需要的内容或堆栈溢出,这可能是由于我的搜索能力很差或因为我想做的事情很具体。

Any help will be deeply appreciated and I will really love to see any similar macro websites out there that I can use as reference. 我们将不胜感激任何帮助,我将非常乐意看到任何类似的宏网站都可以用作参考。 Sorry if this question is already answered. 抱歉,这个问题已经回答。

I'm rewriting this answer as the requirement needs to iterate over any FormatCondition defined for each cell in the range as well as other values of cell. 我正在重写此答案,因为需求需要遍历为范围中的每个单元格以及单元格的其他值定义的任何FormatCondition。 A neat method could be found, for example, here . 例如,可以在此处找到一种整洁的方法。 Basically, it consists of : 基本上,它包括:

For X = 1 To Cell.FormatConditions.Count
    With Cell.FormatConditions(X)
      If .Type = xlCellValue Then
      ...
      If CellInterior Then
          DisplayedColor = IIf(ReturnColorIndex, Cell.Interior.ColorIndex, Cell.Interior.Color)
        Else
          DisplayedColor = IIf(ReturnColorIndex, Cell.Font.ColorIndex, Cell.Font.Color)

Which explores the cell for different ways to define the filling Color. 它将探索以不同方式定义填充颜色的单元格。

So you should iterate over the range 所以你应该遍历整个范围

Dim rng As Range, cell As Range
Set rng = Range("M500:M550")
For Each cell In rng
   rem check the filling with the method in the link
   rem if it's the colorindex you want
   rem cell.Value = ''
   rem Also, get the row number and delete content of range ("C5XX")
Next cell

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM