步骤1:打开Excel并创建一个新的工作表。
步骤2:选择您希望添加下拉列表的单元格。例如,假设您选择A1单元格。
步骤3:按下Alt + F11打开Visual Basic for Applications(VBA)编辑器。
步骤4:在VBA编辑器中,插入一个新的模块。要插入模块,可以在菜单栏上选择“插入”,然后选择“模块”。
步骤5:在新的模块中,编写以下VBA代码:
Sub CreateDropDownList()
Dim dropDownRange As Range
' 设置下拉列表的选项
Set dropDownRange = Worksheets("Sheet1").Range("B1:B3")
' 在选定单元格中创建下拉列表
With Worksheets("Sheet1").Range("A1").Validation
.Delete
.Add Type:=xlValidateList, Formula1:="=" & dropDownRange.Address
.IgnoreBlank = True
.InCellDropdown = True
.ShowInput = True
.ShowError = True
End With
End Sub
步骤6:在代码中,Set dropDownRange = Worksheets("Sheet1").Range("B1:B3")