简体   繁体   中英

Referencing cells from Sheet 1 (Legend) to Sheet 2 (Allocation)

I am trying to use VBA for my final year project. However, I am having some difficulties.

I am trying to use Excel VBA to reference a sheet that I have coded with department names that have their own specific colours. For instance, department "CLR" with red colour. I am hoping that if I were to go to another sheet, and use the drop down list to select the department I want, it will change according to colour I have set from my first sheet.

For the sheet that I have coded, I will be putting below as well as picture files. Do guide me along as I am weak in VBA.

Private Sub Worksheet_Change(ByVal Target As Range)

    Set i = Intersect(Target, Range("A1:Z10000"))
    If Not i Is Nothing Then
        Select Case Target
            Case "CLR": NewColor = 3
            Case "CTS": NewColor = 4
            Case "OMS": NewColor = 5
            Case "ENT": NewColor = 6
            Case "O&G": NewColor = 7
            Case "HND": NewColor = 8
            Case "SUR_ONCO": NewColor = 9
            Case "NES": NewColor = 10
            Case "OTO": NewColor = 11
            Case "PLS": NewColor = 12
            Case "BREAST": NewColor = 13
            Case "UGI": NewColor = 14
            Case "HPB": NewColor = 15
            Case "VAS": NewColor = 16
            Case "H&N": NewColor = 17
            Case "URO": NewColor = 18
            Case "OPEN": NewColor = 19
        End Select
        Target.Interior.ColorIndex = NewColor


    End If


End Sub

Update 2 : Filtering Table

I decided to use a textbox to filter my data when I type in my department. However, I experienced some trouble whenever I type in the department name. Could you possibly help me with my problem?

Private Sub TextBox1_Change()

    Dim Text

    Text = TextBox1.Value

    If Text <> "" Then
        Sheet2.Range("C7:AV26").AutoFilter Field:=1, Criteria1:="Text,_", VisibleDropDown:=False

    Else:
        Sheet2.AutoFilterMode = False

    End If

End Sub

I'm making a couple assumptions about your example, but if it's not exactly what you need I'm hoping you can adapt it. I set up the following range:

在此输入图像描述

Then, on the Allocation worksheet, the single drop-down cell used Data Validation on Cell C2 to validate using a List from =Legend!C2:C6 :

在此输入图像描述

My assumption was that you wanted whatever color you chose for each item on your Legend worksheet to be used for the setting of the drop-down cell on the Allocation worksheet. In your code, you've hard-coded the colors into VBA -- meaning if you wanted to change the colors, you'd have to modify your code. My example below will find the user's selection in the drop-down and grab the current color of that cell to apply it to the drop-down cell. This way, if you want to re-do the colors you don't have to modify your VBA code at all.

The Worksheet_Change event code for the Allocation worksheet looks like this:

Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$C$3" Then
        Dim legendWS As Worksheet
        Dim legendCell As Range
        Set legendWS = ThisWorkbook.Sheets("Legend")

        Set legendCell = legendWS.Range("C2:C6").Find(Target.Value)
        If Not legendCell Is Nothing Then
            Target.Interior.Color = legendCell.Interior.Color
        End If
    End If
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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