从工作表1(图例)到工作表2(分配)
我正在尝试为我的最后一年项目使用VBA。 但是,我有一些困难。
我正在尝试使用Excel VBA引用一个表单,该表单使用了具有自己特定颜色的部门名称。 例如,部门“CLR”红色。 我希望如果我要去另一张纸,并使用下拉列表来select我想要的部门,它会根据我从第一张表中设置的颜色而改变。
对于我已经编码的工作表,我将在下面以及图片文件。 请指导我,因为我在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
更新2:过滤表
当我input部门时,我决定使用文本框来过滤数据。 但是,每次input部门名称时都会遇到一些麻烦。 你能帮我解决我的问题吗?
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
我对你的例子做了一些假设,但如果这不是你所需要的,我希望你能适应它。 我设置了以下范围:
然后,在Allocation
工作表上,单个下拉单元使用Cell C2
上的数据validation来validation,使用来自=Legend!C2:C6
的List
=Legend!C2:C6
:
我的假设是,您需要为“ Legend
工作表上的每个项目select任何颜色,以用于设置Allocation
工作表上的下拉单元格。 在你的代码中,你已经把颜色硬编码到了VBA中 – 这意味着如果你想改变颜色,你必须修改你的代码。 我的下面的例子将在下拉菜单中find用户的select,并抓取该单元格的当前颜色以将其应用于下拉单元格。 这样,如果你想重新做颜色,你根本不需要修改你的VBA代码。
Allocation
工作表的Worksheet_Change
事件代码如下所示:
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