VBA on Excel 删除没有颜色的单元格和相邻单元格
VBA on Excel to delete cells with no color and neighbour cells
所以我试着找出一种方法来使用 VBA 来完成以下技巧:
假设我们有一个巨大的 Excel 文件。
C500 有一些文字。
M500 有一个值,它可以有一个颜色填充,或者只是一个带有数字的默认白色单元格。
我想要实现的是在 M500 没有颜色填充时删除 C500 和 M500。
我知道这是一个简单的任务,至少看起来是这样,而且我知道它可能只需几行代码就可以解决。我仍然无法通过搜索在 google 或堆栈溢出中找到我需要的东西,可能是因为我的搜索技巧很差,或者因为我想做的事情非常具体。
任何帮助将不胜感激,我真的很想看到任何类似的宏网站,我可以将其用作参考。对不起,如果这个问题已经回答了。
我正在重写这个答案,因为要求需要迭代为范围内的每个单元格定义的任何 FormatCondition 以及单元格的其他值。可以找到一个简洁的方法,例如 here。基本上,它包括:
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)
探索单元格的不同方式来定义填充颜色。
所以你应该遍历范围
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
所以我试着找出一种方法来使用 VBA 来完成以下技巧:
假设我们有一个巨大的 Excel 文件。
C500 有一些文字。 M500 有一个值,它可以有一个颜色填充,或者只是一个带有数字的默认白色单元格。 我想要实现的是在 M500 没有颜色填充时删除 C500 和 M500。
我知道这是一个简单的任务,至少看起来是这样,而且我知道它可能只需几行代码就可以解决。我仍然无法通过搜索在 google 或堆栈溢出中找到我需要的东西,可能是因为我的搜索技巧很差,或者因为我想做的事情非常具体。
任何帮助将不胜感激,我真的很想看到任何类似的宏网站,我可以将其用作参考。对不起,如果这个问题已经回答了。
我正在重写这个答案,因为要求需要迭代为范围内的每个单元格定义的任何 FormatCondition 以及单元格的其他值。可以找到一个简洁的方法,例如 here。基本上,它包括:
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)
探索单元格的不同方式来定义填充颜色。
所以你应该遍历范围
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