【发布时间】:2018-04-10 17:25:23
【问题描述】:
我正在使用 Excel 2013,我想在 VBA 中编写一个具有两个参数(Sourcecell 和 Destinationcell)的函数,并且只需将 Backgroundcolor 从 Sourcecell 复制到 Destinationcell。这就是我所拥有的:
Function setRGB2(ByVal sCell As Range, ByVal dCell As Range)
Dim lngColor As Long
Dim B As Long
Dim G As Long
Dim R As Long
On Error GoTo Fehler
lngColor = sCell.Interior.Color
B = lngColor / 65536
G = (lngColor - B * 65536) / 256
R = lngColor - B * 65536 - G * 256
Range(dCell).Interior.Color = RGB(R, G, B)
'Range(dCell).DisplayFormat.Interior.Color = RGB(R, G, B)
Fehler:
With Err
End With
End Function
我得到错误:
不当使用属性
例如,我的 Sourcecell 是 B16,我的 Destinationcell 是 B46。所以在 B46 中我写了=setRGB2($B$16;B46)。我尝试像dCell.Interior.Color = sCell.Interior.Color 那样直接设置颜色,但这不起作用。
编辑
我已经为参数添加了声明。但这似乎是另一个问题。即使我这样做dCell.Interior.ColorIndex = 1,它也会抛出同样的错误。
【问题讨论】:
-
你是如何使用这个功能的,为什么不直接将一个单元格的
Interior.Color分配给另一个单元格呢? -
你为什么不直接使用
Range(dCell).Interior.Color = sCell.Interior.Color?为什么这么复杂?还要在setRGB2(sCell, dCell)中为您的sCell, dCell指定一个类型 -
请声明 sCell 和 dCell,这样我们就知道我们在说什么了。还请确保R大于0,请查看RGB
-
记得检查默认的无颜色,当
sCell.Interior.ColorIndex = xlColorIndexNone
标签: excel excel-2013 vba