Excel 复制单元背景色

Excel 复制单元背景色,excel,excel-2013,vba,Excel,Excel 2013,Vba,我正在使用Excel 2013,我想在VBA中编写一个函数,该函数有两个参数(Sourcecell和Destinationcell),只需将背景色从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 GoT

我正在使用Excel 2013,我想在VBA中编写一个函数,该函数有两个参数(Sourcecell和Destinationcell),只需将背景色从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
,它也会抛出相同的错误。

不确定您希望通过该函数实现什么,但以下代码应该是正确的,至少在语法上是正确的

Option Explicit

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 = WorksheetFunction.Max(lngColor / 65536, 0)
    G = WorksheetFunction.Max((lngColor - B * 65536) / 256, 0)
    R = WorksheetFunction.Max(lngColor - B * 65536 - G * 256, 0)

    dCell.Interior.Color = RGB(R, G, B)
    'Range(dCell).DisplayFormat.Interior.Color = RGB(R, G, B)

    Exit Function

Fehler:
    With Err

    End With
End Function

Sub TestIt()
    setRGB2 Range("A1"), Range("A2")
End Sub

不确定您希望通过函数实现什么,但下面的代码应该是正确的,至少在语法上是正确的

Option Explicit

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 = WorksheetFunction.Max(lngColor / 65536, 0)
    G = WorksheetFunction.Max((lngColor - B * 65536) / 256, 0)
    R = WorksheetFunction.Max(lngColor - B * 65536 - G * 256, 0)

    dCell.Interior.Color = RGB(R, G, B)
    'Range(dCell).DisplayFormat.Interior.Color = RGB(R, G, B)

    Exit Function

Fehler:
    With Err

    End With
End Function

Sub TestIt()
    setRGB2 Range("A1"), Range("A2")
End Sub

用户定义的函数无法更改工作表/单元格的状态。换句话说,不能改变颜色。()

但是Sub可以这样做,因此您可以设计一个函数,然后从Sub调用该函数

但在您的情况下,应该使用带有参数的子对象,并且您可以随时在VBA代码中以简单的方式调用它

Sub testing()

setRGB2 [A1], [A2]

End Sub

Private Sub setRGB2(ByRef sCell As Range, ByRef dCell As Range)

dCell.Interior.Color = sCell.Interior.Color
End Sub
此外,我在开始回答时说,自定义项不能更改工作表的状态,但是如果出于任何原因您确实需要它,那么有一种方法可以以非常复杂和核心的方式来完成

另外,在你的问题中,你说:

例如,我的Sourcecell是B16,Destinationcell是B46。所以在B46中,我写
=setRGB2($B$16;B46)

这是错误的,因为您正在创建循环引用,这会导致错误


用户定义的函数无法更改工作表/单元格的状态。换句话说,不能改变颜色。()

但是Sub可以这样做,因此您可以设计一个函数,然后从Sub调用该函数

但在您的情况下,应该使用带有参数的子对象,并且您可以随时在VBA代码中以简单的方式调用它

Sub testing()

setRGB2 [A1], [A2]

End Sub

Private Sub setRGB2(ByRef sCell As Range, ByRef dCell As Range)

dCell.Interior.Color = sCell.Interior.Color
End Sub
此外,我在开始回答时说,自定义项不能更改工作表的状态,但是如果出于任何原因您确实需要它,那么有一种方法可以以非常复杂和核心的方式来完成

另外,在你的问题中,你说:

例如,我的Sourcecell是B16,Destinationcell是B46。所以在B46中,我写
=setRGB2($B$16;B46)

这是错误的,因为您正在创建循环引用,这会导致错误

首先检查单元格是否有颜色,如果有,请复制:

公共子CopyColor(ByRef源作为范围,ByRef目标作为范围)
如果Source.Interior.ColorIndex=xlColorIndexNone,则
Destination.Interior.ColorIndex=xlColorIndexNone
其他的
Destination.Interior.Color=Source.Interior.Color
如果结束
端接头
不管理渐变或图案。

首先检查单元格是否有颜色,如果有,请复制:

公共子CopyColor(ByRef源作为范围,ByRef目标作为范围)
如果Source.Interior.ColorIndex=xlColorIndexNone,则
Destination.Interior.ColorIndex=xlColorIndexNone
其他的
Destination.Interior.Color=Source.Interior.Color
如果结束
端接头


不管理渐变或图案。

如何使用此功能,为什么不直接将一个单元格的
Interior.Color
分配给另一个单元格?为什么不直接使用
Range(dCell.Interior.Color=sCell.Interior.Color
?为什么这么复杂?在
setRGB2(sCell,dCell)
中为您的
sCell,dCell指定一种类型。请声明sCell和dCell,这样我们就知道我们在谈论什么了。还请确保R大于0,请查看并记住检查默认无颜色,当
sCell.Interior.ColorIndex=xlColorIndexNone
如何使用此函数,为什么不直接将一个单元格的
Interior.Color
分配给另一个单元格?为什么不直接使用
范围(dCell).Interior.Color=sCell.Interior.Color
?为什么这么复杂?在
setRGB2(sCell,dCell)
中为您的
sCell,dCell指定一种类型。请声明sCell和dCell,这样我们就知道我们在谈论什么了。另外,请确保R大于0,请查看并记住检查默认的无颜色,当RGB拆分不必要时,请检查:
sCell.Interior.ColorIndex=xlColorIndexNone
B=(lngColor和rgbBlue)/65536:G=(lngColor和rgbLime)/256:R=(lngColor和RGBREED)
,只要
dCell.Interior.Color=sCell.Interior.Color
就足够了。是的,正如我所说,RGB分割是不必要的。RGB分割是不必要的,但使用它要容易得多:
B=(lngColor和rgbBlue)/65536:G=(lngColor和rgbLime)/256:R=(lngColor和rgbreed)
,只需
dCell.Interior.Color=sCell.Interior.Color
就足够了。是的,正如我所说,RGB分割是不必要的。在UDF上找到更改单元格颜色的好方法!还有一个链接埋在里面,可能更容易实现?我猜是这样的。谢谢你的链接。很高兴在UDF上找到可以改变电池颜色的链接!还有一个链接埋在里面,可能更容易实现?我猜是这样的。谢谢你的链接。