简体   繁体   English

Excel vba 使用 vlookup 复制单元格的颜色

[英]Excel vba using vlookup to copy color of a cell

I am in need of using VLOOKUP to copy the color of a cell (that is not CF).我需要使用 VLOOKUP 来复制单元格的颜色(不是 CF)。 I tried to follow the vba code that is posted in the thread by LondonRob at Vlookup to copy color of a cell - Excel VBA but am having trouble since not proficient in vba.我试图按照 LondonRob 在Vlookup的线程中发布的 vba 代码复制单元格的颜色 - Excel VBA,但由于不精通 vba,因此遇到了麻烦。 Vlookup is needed because Names can be in different order.需要 Vlookup,因为 Names 可以按不同的顺序排列。

I have a sample worksheet where the cell colors need to be copied from the 1st tab to the 2nd tab based on a vlookup of name and column numbers.我有一个示例工作表,其中需要根据名称和列号的查找将单元格颜色从第一个选项卡复制到第二个选项卡。 I set up srcCell and destCell named ranges and copy into a module the posted vba.我设置了 srcCell 和 destCell 命名范围并将发布的 vba 复制到模块中。

1st Tab第一个标签

Name Amt1名称 Amt1

Kathy $500 (cell color red) Kathy $500(电池颜色为红色)

Mark $350 (cell color green)标记 $350(单元格颜色为绿色)

2nd Tab第二个标签

Name Amt1 Amt2名称 Amt1 Amt2

Mark $350 $200 (need $350 in green cell color)标记 $350 $200(绿色单元格颜色需要 $350)

Kathy $500 $400 (need $500 in red cell color) Kathy 500 美元 400 美元(需要 500 美元的红细胞颜色)

Can anyone help?任何人都可以帮忙吗?

If you are unable to understand the solution at: Vlookup to copy color of a cell - Excel VBA , you could try this one instead:如果您无法理解以下解决方案: Vlookup to copy color of a cell - Excel VBA ,您可以试试这个:

It simply uses Match to find and copies the cells with values and format by default.默认情况下,它只是使用Match来查找和复制具有值和格式的单元格。

FirstTab:第一个标签:

第一个选项卡

SecondTab:第二个标签:

第二选项卡

SecondTab after running the macro:运行宏后的 SecondTab:

运行宏后的 SecondTab

Sub copy_paste_with_format()

    Dim i As Long
    Dim var As Variant

    Dim FirstTab As Worksheet
    Dim SecondTab As Worksheet

    Set FirstTab = Application.Worksheets("FirstTab")
    Set SecondTab = Application.Worksheets("SecondTab")

    For i = 2 To 3
        var = Application.Match(SecondTab.Range("A" & i), FirstTab.Range("A:A"), 0)
        If Not IsError(var) Then
            FirstTab.Range("B" & var).Copy SecondTab.Range("B" & i)
        End If
    Next i

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM