VBA基于下拉菜单中的selectdynamicvalidation文本框

所有。 我在工作表中有一个名为lookupMisc的表,名为periodNo(A)periodName(B) ,其值为以下值。

AB 1 09:00 2 10:00 3 11:00 4 12:00 5 13:00 6 14:00 7 15:00 8 16:00 9 17:00 

代码如下:

  For i = 1 To ws_misc.Range("periodNo").Rows.Count CombinedName = periodNo(i, 1) & " - " & periodName(i, 1) cbo_period.AddItem CombinedName Next i 

和thsi产生:

在这里输入图像说明

现在你会注意到右边有一个Lengthtxt_length )文本字段。 基本上我正在尝试创build一个基于期限条目的dynamicvalidation的forms。 这里是情况:

 If user selects Then 1 - 09:00 Length can only accept values from 1-9 2 - 10:00 Length can only accept values from 1-8 3 - 11:00 Length can only accept values from 1-7 4 - 12:00 Length can only accept values from 1-6 5 - 13:00 Length can only accept values from 1-5 6 - 14:00 Length can only accept values from 1-4 7 - 15:00 Length can only accept values from 1-3 8 - 16:00 Length can only accept values from 1-2 9 - 17:00 Length can only accept values from 1-1 

我将如何去完成这个?

尝试像这样:

 if me.txt_length.value<1 or me.txt_length.value>10-clng(left(me.cbo_period.value,1)) then msgbox "Entry must be between 1 and " & 10-clng(left(me.cbo_period.value,1)) end if 

您可以将数据validation放在基于下拉列表中所选值的长度单元格中:

DataValidationExample1

DataValidationExample2

祝你好运。