我正试着在Excel 2016中做一个下拉列表,但由于不经常使用,我现在不知道该怎么做。
数据如下所示:
我正在尝试创建一个下拉列表,它将包含基于E列单元格值的"Title 1","Title2","Title3“的值。
换句话说,就像这样:
if Cell E == 'index'
add Cell A value to dropdown
end
实现这一目标的最佳方法是什么?我可以使用excel函数来完成吗?还是需要VBA?如果需要VBA,任何提示都将不胜感激。
发布于 2017-02-27 18:58:29
如果您正在处理excel UI中的下拉式单元格,则可以使用以下代码(请参阅注释以根据实际需要对其进行调整):
Sub Main()
With Worksheets("sheetWithDropDownName").Range("A1").Validation '<--| change "sheetWithDropDownName" and "A1" to your actual "dropdown" worksheet and cell references
.Delete
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
xlBetween, Formula1:=Join(GetTitles(), ",")
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = ""
.InputMessage = ""
.ErrorMessage = ""
.ShowInput = True
.ShowError = True
End With
End Sub
Function GetTitles() As Variant
Dim cell As Range, rng As Range
With Worksheets("sheetWithDataName") '<--| change "sheetWithDataName" to your actual sheet with data name
Set rng = .Range("A1", .cells(.Rows.Count, "E").End(xlUp)) '<--| set a range as its columns A:E range from row 1 down to last column E not empty row
End With
With CreateObject("Scripting.Dictionary")
For Each cell In rng.Columns(5).cells
If cell.Value = "index" Then .Item(cell.Offset(, -4).Value) = cell.Offset(, -4).Value
Next cell
GetTitles = .keys
End With
End Function
发布于 2017-02-27 18:33:20
使用VBA,这样的事情可能会起作用。组合框应该是一个表单,数据应该在一个名为"MyData“的工作表中。
Private Sub UserForm_Initialize()
Dim lLastRow As Long
Dim i As Integer
lLastRow = Worksheets("MyData").Cells(Rows.Count, 1).End(xlUp).Row
For i = 2 To lLastRow
If Worksheets("MyData").Cells(i, 5) = "index" Then
ComboBox1.AddItem (Worksheets("MyData").Cells(i, 1))
End If
Next
End Sub
https://stackoverflow.com/questions/42482997
复制相似问题