限制用户窗体对特定Excel单元格中的值的响应

我在Excel中创build了一个VBA Userform,并且希望限制可以input到标记为“Date”的文本框的date列表到另一个工作表上的单元格中列出的date。

这可能吗? 或者从特定单元格填充列表框?

使用ComboBoxListBox控件而不是TextBox控件好得多

 Option Explicit Sub main() With MyUserForm '<--| change "MyUserForm" to your actual userform name .DatesCB.List = GetDates '<--| change "DatesCB" to your actual dates ComboBox name .Show End With End Sub Function GetDates() With Worksheets("Dates") '<--| reference "Dates" worksheet (change "Dates" to your actual dates worksheet name) GetDates = Application.Transpose(.Range("A2", .Cells(.Rows.Count, "A").End(xlUp)).Value) '<--|change "A"'s to your actual column index with dates End With End Function 

将combobox或列表框的行来源属性设置为范围的外部地址将使用该范围的值填充控件。 这将适用于单列或多列范围。

 Private Sub UserForm_Initialize() ListBox1.RowSource = Worksheets("Sheet1").Range("A1:A10").Address(External:=True) End Sub