0

我有以下问题。我想调用一个子程序来更改单元格范围的背景颜色。用cells(1,1)计算单元格范围,然后计算地址以接收A1。

在调用子程序之前,我得到了单元格的地址,如下所示:

Range1 = cells(4, 4).Address(RowAbsolute:=False, ColumnAbsolute:=False)
Range2 = cells(4, CellAmount - 1).Address(RowAbsolute:=False, ColumnAbsolute:=False)

我想我需要这个,因为子程序是这样声明的:

Sub SetBGLightGrey(cells As String)

    range(cells).Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 15921906
        .TintAndShade = 0
       .PatternTintAndShade = 0
    End With

End Sub

范围 1 和范围 2 是字符串,我将其连接到范围声明:

RangeArea = """" & Range1 & ":" & Range2 & """"

当我这样调用我的子程序时:

Call SetBGLightGrey(RangeArea)

我收到以下错误消息:

“运行时错误'1004':对象'_Global'的方法'Range'失败。我不明白,因为如果我用正确的单元格值调用子例程:

Call SetBGLightGrey("D4:K4")

有用。它是字符串并且具有相同的值。这根本不可能可以吗?

4

1 回答 1

2

您不需要 RangeArea 周围的引号。

RangeArea = Range1 & ":" & Range2

But then, why would you want to pass ranges around as strings and then convert them back to ranges? Pass the range objects all the time.

Sub SetBGLightGrey(byval cells as range)
  With cells.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 15921906
    .TintAndShade = 0
    .PatternTintAndShade = 0
  End With
End Sub 

SetBGLightGrey range(cells(4, 4), cells(4, CellAmount - 1))
于 2011-06-07T11:21:15.100 回答