来自另一个工作簿VBA的Vlookup单元格背景色

时间:2018-12-05 16:54:12

标签: vba excel-vba colors excel-formula vlookup

因此,我有一个像工作簿这样的Gant图表,该图表对每个工作人员都有一个唯一的编号(代表一行),然后我想从此行和指定的列中检索单元格颜色,就像正常的vlookup一样。参见下图。

enter image description here

我希望在与日期(即列和行)即工作号相对应的单元格中拾取绿色。

谢谢

1 个答案:

答案 0 :(得分:1)

我不确定您在做什么,但是因为我仍然有这段代码...

Function Tester(rngLookup As Range, v)
    Dim c As Range, f As Range, clr As Long

    Set c = Application.ThisCell '<< the cell with the formula
    Set f = rngLookup.Find(v, lookat:=xlWhole)
    If Not f Is Nothing Then
        clr = f.Interior.Color
    Else
        clr = vbWhite
    End If
    'change the background for the cell with the formula
    Application.Evaluate "ChangeColor(""" & c.Parent.Name & """,""" & c.Address() & """," & clr & ")"
    Tester = v 'or whatever is appropriate...
End Function

Sub ChangeColor(sht As String, addr As String, clr As Long)
    ThisWorkbook.Sheets(sht).Range(addr).Interior.Color = clr
End Sub

用法示例(启用了显示公式):

enter image description here