问题
I have the following data in a worksheet called Parts.
In a different worksheet called Planning, I have the following data:
In the above Planning Worksheet, Cell D3 is a dropdown that allow to choose a display language. The current choices are "English" and "Japanese". The A column's cells are also dropdown that allow to select a dimension.
What I want to do is create a dropdown that:
- is dependent on the cell in the A column. The dropdown should filtered data from the Parts Worksheet according to the value of the corresponding A cell.
- is also dependent on the D3 Cell. The dropdown should display "English description" if D3 is "English" or "Japanese description" if D3 is "Japanese"
- once selected, the data in the dropdown should be the Part and not the description. In other words, it should behave like a select tag in HTML.
I am new to VBA and after quite a lot of searching, I can't figure out how to do this. I would really appreciate a detailed answer. Thank you in advance!
EDIT:
The final Parts Worksheet will be at least 10,000 lines long. The user can't create named list manually. For this reason, I think I should use VBA.
回答1:
I'm not sure if you tried this when I posted it as an answer on your question from yesterday.
The code does everything that you need by creating a validation dropdown on-the-fly based on the value in column A when you select a cell on column B. The dropdown displays the product code and the description depending on the language. The description is removed once a product code has been selected and the validation is removed from the cell.
While the code does do everything that you require it's not perfect but it gives you a huge head start and it should work with your sheet names etc. if you copy and paste it and give it a try.
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
来源:https://stackoverflow.com/questions/42510322/how-to-create-complex-dependent-dropdown-using-vba-in-excel-2016