[英]Referencing cells from Sheet 1 (Legend) to Sheet 2 (Allocation)
I am trying to use VBA for my final year project. 我正在尝试将VBA用于我的最后一年项目。 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. 我正在尝试使用Excel VBA来引用我使用具有自己特定颜色的部门名称编写的工作表。 For instance, department "CLR" with red colour.
例如,红色部门“CLR”。 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.
因为我在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 更新2:过滤表
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
: 然后,在
Allocation
工作表上,单个下拉单元格使用Cell C2
上的数据验证来使用来自=Legend!C2:C6
的List
进行验证=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. 我的假设是,您希望为
Legend
工作表中的每个项目选择的颜色用于设置Allocation
工作表上的下拉单元格。 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. 在您的代码中,您已将颜色硬编码为VBA - 这意味着如果您想要更改颜色,则必须修改代码。 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.
这样,如果要重新执行颜色,则根本不需要修改VBA代码。
The Worksheet_Change
event code for the Allocation
worksheet looks like this: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.