【发布时间】:2021-08-19 15:20:57
【问题描述】:
我有一些 VBA 可以根据单元格的颜色清除指定范围内的单元格。这些细胞是硬色的。它可以在没有未合并单元格的情况下工作,但是一旦我在范围内添加合并单元格,它就会失败。
我一直在做一些挖掘,似乎我需要使用 Cells 来引用合并中的第一个单元格。由于我对 VBA 不熟悉,所以不知道如何合并这两段代码。
如果您能提供任何帮助、建议或指向其他解决方案的链接,我们将不胜感激。
当前有效的代码:
Sub ClearContentByBackground()
For Each Cell In Range("b1:i32")
If Cell.Interior.Color = RGB(226, 239, 218) Then
Cell.ClearContents
End If
Next
End Sub
Code that I found during my search:
If Cells(j, l).MergeCells Then
Cells(j, l).MergeArea.ClearContents
Else
Cells(j, l).ClearContents
End If
【问题讨论】: