首页 > 解决方案 > 从其他文件中查找并保持格式

问题描述

我有 33 个不同公司部门的文件,我们在其中保留了员工的工作时间。他们每个人都很少有人在那里工作。

我编写了一个“主”excel,它保存了这 33 个不同文件中的所有数据。我设法编写了几个公式来保持它正确加载数据。但我只有正确的价值观。

我仍然需要加载对我们至关重要的单元格颜色。你有什么想法如何实现吗?我已经看过那个话题,但我不能让它继续工作。也在考虑将文件与 VBA 合并,但没有任何运气 - 我仍然只得到值,而不是单元格的颜色。

只是我正在做的一个示例

标签: excelvbavlookup

解决方案


您可以尝试使用此 UDF 来获取单元格颜色和格式

复制下面的代码Module1,

使用 LookupFormat 作为公式 =LookupFormat(A1,Sheet2!A1:D100,4) 这将获取值以及单元格格式

Public IntClr, IntShade, FntClr, FntShade, CellAdd
Function LookupFormat(ByRef FndValue, ByRef LookupRng As Range, ByRef ColRef As Long)
    Set FindCell = LookupRng.Find(What:=FndValue, LookIn:=xlValues)
    CellAdd = Application.Caller.Address
    With FindCell
        FindRow = .Row
        FindCol = .Column
        With .Offset(0, ColRef - 1)
            With .Interior
                IntClr = .Color
                IntShade = .TintAndShade
            End With
            With .Font
                FntClr = .Color
                FntShade = .TintAndShade
            End With
            LookupFormat = .Value
        End With
    End With
End Function

将下面的宏复制到工作表模块

Sub Worksheet_Change(ByVal Target As Range)
    If CellAdd = "" Then Exit Sub
    RefChk = IntClr & IntShade & FntClr & FntShade
    If RefChk = "" Then Exit Sub
    With Range(CellAdd)
        With .Interior
            .Color = IntClr
            .TintAndShade = IntShade
        End With
        With .Font
            .Color = FntClr
            .TintAndShade = FntShade
        End With
    End With
    IntClr = ""
    IntShade = ""
    FntClr = ""
    FntShade = ""
    CellAdd = ""
End Sub

参考:https ://quadexcel.com/wp/vlookup-get-cell-color-font-color-along-lookup-value/

编辑:更新了如何使用该 UDF,感谢 Tim


推荐阅读