如果在combobox中没有范围要加载,则限制用户窗体加载的代码

我已经开发了一个小型用户窗体,其中包含一个combobox。 加载用户窗体时,此combobox在工作表中填充范围。 我需要的是,如果没有值加载combobox,它应该显示一个错误“没有什么可以加载”。 这个用户表单通过一个小button加载。

以下是我的用户表单初始化代码:

Private Sub UserForm_Activate() 'LOAD THE LIST OF ACCOUNTS 'ASSIGNING THE VARIABLES Dim ws As Worksheet Dim tbl As ListObject Dim rng As Range 'Declaring the Variables Set ws = Sheets("Cash and Bank Account Details") Set tbl = ws.ListObjects("newaccount") Set rng = tbl.ListColumns(3).DataBodyRange 'Adding the Items in Combo Box For Each rng In rng ComboBox1.AddItem rng.Value Next ComboBox1.ListIndex = 0 End Sub 

请审查和帮助我的查询。

谢谢。

我猜你正在寻找这样的东西:

 If UserForm1.ComboBox1.ListCount = 0 Then MsgBox "Nothing is there to load" Else UserForm1.Show End If 

在向用户显示表单之前,您可以检查ComboBox1中是否有值得显示的内容。

或者,您甚至可以在初始化表单之前进行检查:

 If Application.WorksheetFunction.CountA(Sheets("Cash and Bank Account Details").Listobjects("newaccount").ListColumns(3).DataBodyRange) = 0 Then MsgBox "Nothing is there to load" Else 'Initialize and fill the form UserForm1.Show End If