我有一个下拉列表,显示来自另一个工作表的合并零件库。这个工作表将不断更新,直到我们可以获得一个有效的数据库,因此零件清单的功能或多或少都是一成不变的。
我已经连接了一些数据以形成一个命名表`ProjectTab,看起来像这样:
为了让工作表自动填充,我缩短了数据验证列表的输出,只输出Part Index
,如下所示:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
Dim textVal As String
Dim strVal As String
strVal = Target.Value
If Target.Column = 2 Then
textVal = Left(strVal, 9)
Target.Value = textVal
End If
End Sub
以下IF
声明允许我弄清楚该部件的产品类型
=IF(B2>0,IF(LEFT(D2,1)="1","Assembly",IF(LEFT(D2,1)="2","Sub-Assembly",IF(LEFT(D2,1)="3","Software",IF(LEFT(D2,1)="4","Hardware",IF(LEFT(D2,1)="5","Chemical",IF(LEFT(D2,1)="6","Spare",IF(LEFT(D2,1)="7","Spare",IF(LEFT(D2,1)="8","Document",IF(LEFT(D2,1)="9","Misc",""))))))))),"")
其中产生以下列:
我想使用类似于上述IF
语句的内容来过滤下拉列表以选择部分,即我通过工作表顶部的过滤器预选Assembly Type
-turn将Part List
下拉列表过滤为相应的类型。
这可能吗?如果是这样我怎么能这样做?
谢谢,
丹
答案 0 :(得分:1)
更新了答案
我写了一个我觉得适合你的VBA示例。您可以下载示例here,但如果您愿意,也可以根据以下示例构建自己的工作表。
对于此示例,您需要按如下方式设置工作簿。
将其复制并粘贴到“示例”工作表代码中。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim AssemblyFilter As Range, PartsDropDown As Range
Dim strList As String, strAssemblyFilter As String
Dim i As Long
Set AssemblyFilter = Range("B1") 'Set this to the cell that has the Assembly Filter
strAssemblyFilter = AssemblyFilter.Value
If Not Intersect(Target, AssemblyFilter) Is Nothing Then 'Only Run following code when Assembly Filter cell has changed
strList = ""
With Sheets("Parts Library") 'Sheet with Parts Library
For i = 1 To .Cells(Rows.Count, "A").End(xlUp).Row
If strAssemblyFilter = .Cells(i, "B").Value Then 'Compare Assembly Filter to values in Column "B"
strList = strList & "," & .Cells(i, "A").Value 'If filter matches, then append this to the data Validation List
End If
Next i
strList = Mid(strList, 2) 'Chop off leading comma
End With
'Apply Data Validation to this Range (starting at cell B4 and ending at the last row with data in column A)
Set PartsDropDown = Range("B4:B" & Cells(Rows.Count, "A").End(xlUp).Row)
Application.EnableEvents = False
With PartsDropDown.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=strList 'Pass in Validation List created above
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
Application.EnableEvents = True
End If
End Sub
当您更改装配过滤器并触发Worksheet_Change
事件时,代码首先根据装配过滤器生成零件清单,然后重置并创建一个新的验证过滤器,从B4开始到最后一行结束使用A列中的数据。您可以根据需要进行调整,如果愿意,甚至可以对范围进行硬编码。
希望这会有所帮助,如果您在修改此内容时遇到任何问题请与我联系。