Excelmacrosinputtypes不返回正确的值

我试图删除使用下面的代码在Excel 2010工作表中具有相同的内部颜色的所有单元格:

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 

但是当我运行代码时,出现以下错误:

 Method 'Range' of object '_Global' failed. 

我已经发现,即使我要求单元格引用作为范围对象( Type:=8 ), myRange被设置为单元格的值。 例如, A2的值是“testing”。 myRange应该回来为A2 ,但是它回来作为“testing”。 任何想法为什么会这样?

做就是了:

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

您已经将myRange为Rangevariables,因此您不需要将其限定为Range(myRange) 。 如果你这样做,它正在试图评估myRange.Value ,这就是为什么你得到的错误。

干杯。

你的问题是与范围(myRange)。 Range对象可以以两种方式之一使用; (即Range(“A1:B2”))或其他两个Range对象,代表矩形的左上angular和右下angular(即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