如何在Excel 2016中使用VBA创建复杂的相关下拉列表?
我在名为“零件”的工作表中有以下数据 在另一个名为“规划”的工作表中,我有以下数据: 在上面的计划工作表中,单元格D3是一个下拉列表,允许选择显示语言。目前的选择是“英语”和“日语”。A列的单元格也是允许选择标注的下拉列表 我想做的是创建一个下拉列表:如何在Excel 2016中使用VBA创建复杂的相关下拉列表?,vba,excel,dropdown,Vba,Excel,Dropdown,我在名为“零件”的工作表中有以下数据 在另一个名为“规划”的工作表中,我有以下数据: 在上面的计划工作表中,单元格D3是一个下拉列表,允许选择显示语言。目前的选择是“英语”和“日语”。A列的单元格也是允许选择标注的下拉列表 我想做的是创建一个下拉列表: 依赖于A列中的单元格。下拉列表应根据相应A单元格的值过滤零件工作表中的数据 也依赖于D3电池。如果D3为“英语”,下拉菜单应显示“英语描述”;如果D3为“日语”,下拉菜单应显示“日语描述” 选择后,下拉列表中的数据应为零件,而不是描述。换句话
最终零件工作表至少有10000行。用户无法手动创建命名列表。出于这个原因,我认为我应该使用VBA。我不确定昨天我把它作为你问题的答案发布时你是否尝试过 当您在B列中选择单元格时,代码会根据a列中的值动态创建一个验证下拉列表,从而完成所需的所有操作。该下拉列表根据语言显示产品代码和说明。选择产品代码并从单元格中删除验证后,将删除描述 虽然代码做了你需要的一切,但它并不完美,但它给了你一个巨大的开端,它应该与你的工作表名称等工作。如果你复制粘贴它,并尝试一下
Dim CHANGING_VAL As Boolean 'Global Variable that can be set to prevent the onchange being fired when the Macro is removing the description from the dropdown.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 2 And CHANGING_VAL = False Then
CHANGING_VAL = True
If InStr(1, Target.Value, "~") > 2 Then
Target.Value = Left(Target.Value, InStr(1, Target.Value, "~") - 2)
End If
Target.Validation.Delete
Target.Font.Color = RGB(0, 0, 255)
CHANGING_VAL = False
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 2 Then
If Target.Offset(0, -1) <> "" Then
strValidList = ""
For intRow = 1 To 10000
If Sheets("Parts").Cells(intRow, 1) = Target.Offset(0, -1) Then
If Sheets(Target.Parent.Name).Cells(3, 4) = "English" Then
strValidList = strValidList & Sheets("Parts").Cells(intRow, 2) & " ~ " & Sheets("Parts").Cells(intRow, 3) & ", "
Else
strValidList = strValidList & Sheets("Parts").Cells(intRow, 2) & " ~ " & Sheets("Parts").Cells(intRow, 4) & ", "
End If
End If
Next
If strValidList <> "" Then
strValidList = Left(strValidList, Len(strValidList) - 2)
Target.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=strValidList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
End If
Else
Sheets(Target.Parent.Name).Range("B:B").Validation.Delete
End If
End Sub
Dim CHANGING_VAL As Boolean'全局变量,可设置该变量以防止在宏从下拉列表中删除描述时触发onchange。
私有子工作簿(ByVal Sh作为对象,ByVal目标作为范围)
如果Target.Column=2且CHANGING_VAL=False,则
更改值=真
如果指令(1,目标值,“~”)大于2,则
Target.Value=Left(Target.Value,InStr(1,Target.Value,“~”)-2)
如果结束
Target.Validation.Delete
Target.Font.Color=RGB(0,0255)
更改值=False
如果结束
端接头
私有子工作簿\u Sheet SelectionChange(ByVal Sh作为对象,ByVal目标作为范围)
如果Target.Column=2,则
如果目标偏移量(0,-1)”,则
strValidList=“”
对于intRow=1到10000
如果板材(“零件”)单元格(intRow,1)=目标偏移量(0,-1),则
如果Sheets(Target.Parent.Name).Cells(3,4)=“English”,则
strValidList=strValidList&Sheets(“Parts”).单元格(intRow,2)和“~”&Sheets(“Parts”).单元格(intRow,3)和“,”
其他的
strValidList=strValidList&Sheets(“Parts”).单元格(intRow,2)和“~”&Sheets(“Parts”).单元格(intRow,4)和“,”
如果结束
如果结束
下一个
如果列表为“”,则
strValidList=Left(strValidList,Len(strValidList)-2)
目标。选择
选择。验证
.删除
.Add类型:=xlValidateList,AlertStyle:=xlValidAlertStop,运算符:=xlBetween,公式1:=strValidList
.IgnoreBlank=True
.InCellDropdown=True
.InputTitle=“”
.ErrorTitle=“”
.InputMessage=“”
.ErrorMessage=“”
.ShowInput=True
.ror=真
以
如果结束
如果结束
其他的
工作表(Target.Parent.Name).范围(“B:B”).验证.Delete
如果结束
端接头
我不确定昨天我把它作为你问题的答案发布时你是否尝试过这个
当您在B列中选择单元格时,代码会根据a列中的值动态创建一个验证下拉列表,从而完成所需的所有操作。该下拉列表根据语言显示产品代码和说明。选择产品代码并从单元格中删除验证后,将删除描述
虽然代码做了你需要的一切,但它并不完美,但它给了你一个巨大的开端,它应该与你的工作表名称等工作。如果你复制粘贴它,并尝试一下
Dim CHANGING_VAL As Boolean 'Global Variable that can be set to prevent the onchange being fired when the Macro is removing the description from the dropdown.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 2 And CHANGING_VAL = False Then
CHANGING_VAL = True
If InStr(1, Target.Value, "~") > 2 Then
Target.Value = Left(Target.Value, InStr(1, Target.Value, "~") - 2)
End If
Target.Validation.Delete
Target.Font.Color = RGB(0, 0, 255)
CHANGING_VAL = False
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
If Target.Column = 2 Then
If Target.Offset(0, -1) <> "" Then
strValidList = ""
For intRow = 1 To 10000
If Sheets("Parts").Cells(intRow, 1) = Target.Offset(0, -1) Then
If Sheets(Target.Parent.Name).Cells(3, 4) = "English" Then
strValidList = strValidList & Sheets("Parts").Cells(intRow, 2) & " ~ " & Sheets("Parts").Cells(intRow, 3) & ", "
Else
strValidList = strValidList & Sheets("Parts").Cells(intRow, 2) & " ~ " & Sheets("Parts").Cells(intRow, 4) & ", "
End If
End If
Next
If strValidList <> "" Then
strValidList = Left(strValidList, Len(strValidList) - 2)
Target.Select
With Selection.Validation
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:=strValidList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End If
End If
Else
Sheets(Target.Parent.Name).Range("B:B").Validation.Delete
End If
End Sub
Dim CHANGING_VAL As Boolean'全局变量,可设置该变量以防止在宏从下拉列表中删除描述时触发onchange。
私有子工作簿(ByVal Sh作为对象,ByVal目标作为范围)
如果Target.Column=2且CHANGING_VAL=False,则
更改值=真
如果指令(1,目标值,“~”)大于2,则
Target.Value=Left(Target.Value,InStr(1,Target.Value,“~”)-2)
如果结束
Target.Validation.Delete
Target.Font.Color=RGB(0,0255)
更改值=False
如果结束
端接头
私有子工作簿\u Sheet SelectionChange(ByVal Sh作为对象,ByVal目标作为范围)
如果Target.Column=2,则
如果目标偏移量(0,-1)”,则
strValidList=“”
对于intRow=1到10000
如果板材(“零件”)单元格(intRow,1)=目标偏移量(0,-1),则
如果Sheets(Target.Parent.Name).Cells(3,4)=“English”,则
strValidList=strValidList&Sheets(“Parts”).单元格(intRow,2)和“~”&Sheets(“Parts”).单元格(intRow,3)和“,”
其他的
strValidList=strValidList和图纸(“