简体   繁体   English

VBA根据不同工作表上范围内的值更改单元格颜色

[英]VBA change cell color based on value in ranges on different sheets

I have a main form (Sheet2) where the users enter a long code in column C and in column D there is a formula set to take just the last 6 digits of the long code. 我有一个主表格(Sheet2),在该表格中,用户在C列中输入一个长代码,而在D列中,有一个公式集仅接受长代码的最后6位数字。 If those values derived from the formula in cell D equal any values listed in column C of a separate sheet called "ref_list" (Sheet5) then I'd like the corresponding cell in the F column of the main sheet to turn red. 如果从单元格D中的公式得出的那些值等于另一页名为“ ref_list”(Sheet5)的工作表的C列中列出的任何值,那么我希望主工作表F列中的相应单元格变为红色。 Once the cell color is red, the user will be able to click on the red colored cell in column F and be navigated to a list of values associated with the 6 digit value in column D. 单元格颜色为红色后,用户将能够单击F列中的红色单元格,并导航到与D列中的6位数值关联的值列表。

So, if the 6 digit code derived in column D is "123ABC" and that value is equal to one of the codes listed in column C of Sheet5, then I'd like the F column cell of the same row to turn red and be clickable. 因此,如果在D列中得出的6位代码是“ 123ABC”,并且该值等于Sheet5的C列中列出的代码之一,那么我希望同一行的F列单元格变为红色并变为点击。 Once clicked it will trigger a macro listing all values associated with "123ABC". 单击后,将触发一个宏,列出与“ 123ABC”关联的所有值。

Right now, I have hard coded these values and the macros associated with them. 现在,我已经硬编码了这些值以及与它们关联的宏。 I don't want to hard code these values so I put them in a range. 我不想对这些值进行硬编码,因此将它们放在一个范围内。 But, I am having trouble getting the logic to work. 但是,我很难使逻辑起作用。 Once the range is set up, how can I specify which cells to turn red and how to trigger the proper macro associated with the 6 digit values. 设置范围后,如何指定要变成红色的单元格以及如何触发与6位数字值相关的正确宏。 I have researched range in vba but on how to call macros based on range values, I have't had much luck finding any resources. 我已经在vba中研究了range,但是关于如何根据范围值调用宏的方法,我没有太多运气可以找到任何资源。 Here is the code I have so far for it. 这是到目前为止的代码。

 Sub cellColorChange()

Dim acctCode As Range
Set acctCode = Sheet2.Range("D7:D446").Value

Dim refCodes As Range
Set refCodes = Sheet5.Range("C1:C20").Value

Dim changeColor As Range
Set changeColor = Sheet2.Range("F7:F446").Value

If acctCode.Value = refCodes.Value Then
changeColor.ActiveCell.Interior.Color = 3
Else
ActiveCell.Interior.Color = 0
End If

End Sub

My setup is Sheet2 and Sheet5 ("ref_list") - both sheets have headers 我的设置是Sheet2和Sheet5(“ ref_list”)-两个工作表都有标题

This is what the code bellow does 这是下面的代码

Sheet2 (main sheet) Sheet2(主表)

X1


Sheet5 ("ref_list") Sheet5(“ ref_list”)

X2


Execute main sub ShowCells() - result 执行主子ShowCells()-结果

X3


Clicking the link in F3 filters all items containing value "123BCD" 单击F3中的链接将筛选所有包含值“ 123BCD”的项目

X4


Clicking the link in F3 again (in filter mode), clears the filter 再次单击F3中的链接(在过滤器模式下),清除过滤器

X5


Code


A new VBA module containing these 2 subs: 一个新的VBA模块,其中包含以下2个子:

Option Explicit

Private Const S2 = "Sheet2" 'Name of the main sheet
Private Const WS1_COL = 4   'Column in main sheet (D)
Private Const WS1_F = 6     'Column in main sheet (F)
Private Const WS2_COL = 3   'Column in ref_list sheet (C)

Public Sub ShowCells()
    Dim ws1 As Worksheet, ws2 As Worksheet, r1 As Long, r2 As Long
    Dim ur1 As Variant, ur2 As Variant, ub2 As Long, cel As Range, lnk As Range

    Set ws1 = ThisWorkbook.Sheets(S2)
    Set ws2 = ThisWorkbook.Sheets("ref_list")

    ur1 = ws1.UsedRange.Columns(WS1_COL)
    ur2 = ws2.UsedRange.Columns(WS2_COL)
    ub2 = UBound(ur2)

    Application.ScreenUpdating = False
    For r1 = 2 To UBound(ur1)
        For r2 = 2 To ub2
            If ur1(r1, 1) = ur2(r2, 1) Then
                Set cel = ws1.Cells(r1, WS1_COL)
                Set lnk = cel.Offset(0, WS1_F - WS1_COL)
                lnk.Interior.ColorIndex = 3
                ws1.Hyperlinks.Add Anchor:=lnk, Address:="", SubAddress:=cel.Address
                Exit For
            End If
        Next
    Next
    Application.ScreenUpdating = True
End Sub

Public Sub ShowRefs(ByVal id As String)
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets(S2)
    ws.UsedRange.Columns(WS1_COL).AutoFilter Field:=1, Criteria1:=ws.Range(id).Value
End Sub

In VBA module for main sheet (Sheet2) 在主表的VBA模块中(Sheet2)

Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
    If Me.AutoFilter Is Nothing Then
        ShowRefs Target.SubAddress
    Else
        Me.UsedRange.AutoFilter
    End If
End Sub

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

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