VBA如何检查双列组合的值

时间:2018-12-17 09:17:26

标签: excel vba excel-vba

我有一个双列组合,我使用column(1)中的值作为答案,但是当我想使用此代码检查该组合是否具有值时,它不会执行,因此当我尝试从中检索该值时一个组合,我得到运行时错误381。 这是我的代码

 If Trim(CmBCodes.Value) = "" Then
    MsgBox "Please fill in the journey Code"
    CmBCodes.SetFocus
    Exit Sub
    End If

并将其更改为此

   If Trim(CmBCodes.Value) = Null And Len(CmBCodes.Value)) = Null Then
    MsgBox "Please fill in the journey Code"
    CmBCodes.SetFocus
    Exit Sub
    End If

并且即使该组合没有选择任何值,该代码也不会执行,因此它一直到我尝试检索该值的代码段并在此处抛出错误381

  ws.Cells(iRow, 2).Value = UCase(CmBCodes.Column(1))

1 个答案:

答案 0 :(得分:2)

如果未进行选择,则组合框和列表框的listindex均为-1

If CmBCodes.ListIndex = -1  Then
    MsgBox "Please fill in the journey Code"
    CmBCodes.SetFocus
    Exit Sub
End If