0

I'm trying to delete all cells that have the same interior color in an Excel 2010 worksheet using the following code:

Dim myRange As Range
Set myRange = Application.InputBox("Select a cell to remove based on background fill color.", Type:=8)
Range("C3").Interior.Color = Range(myRange).Interior.Color

But when I run the code, I get the following error:

Method 'Range' of object '_Global' failed.

I've figured out that even though I'm asking for the cell reference as a range object (Type:=8), myRange is being set to the value of the cell. For example, the value in A2 is "Test." myRange should come back asA2, but it's coming back as "Test." Any idea why that would be?

4

2 回答 2

2

做就是了:

Range("C3").Interior.Color = myRange.Interior.Color

您已经将尺寸标注myRange为 Range 变量,因此无需将其限定为Range(myRange). 如果您这样做,它会尝试评估myRange.Value,这就是您收到错误的原因。

干杯。

于 2013-05-15T15:50:55.747 回答
2

你的问题是范围(myRange)。Range 对象可以以两种方式之一使用;一个字符串(即 Range("A1:B2"))或两个其他 Range 对象,表示矩形的左上角和右下角(即 Range(Cells(1,1),Cells(2,2)))。您正在为它提供一个 Range 对象。

我怀疑你打算这样做:

Dim myRange As Range
Set myRange = Application.InputBox("Select a cell to remove based on background fill color.", Type:=8)
Range("C3").Interior.Color = myRange.Interior.Color
于 2013-05-15T15:51:01.730 回答