【问题标题】:Code to restrict userform from loading if there is no range to be loaded in combobox如果组合框中没有要加载的范围,则限制加载用户表单的代码
【发布时间】:2016-03-19 19:10:48
【问题描述】:

我开发了一个包含一个组合框的小型用户表单。此组合框在加载用户表单时填充工作表中的范围。我需要的是,如果组合框中没有要加载的值,它应该显示错误“没有要加载的内容”。这个用户表单是通过一个小按钮加载的。

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

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 ComboBox1.Value = "" Then Combobox1.Value = "Nothing is there to load" End If 在您的Combobox1.ListIndex = 0 之后
  • 如果没有阻塞则显示错误结束

标签: excel vba


【解决方案1】:

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

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

【讨论】:

  • 当没有账户加载时会显示消息,但如果有信息加载也会显示相同的消息。
  • 您正在使用这两种解决方案中的哪一种?请注意,我更新了第二个解决方案(检查表“现金和银行账户详细信息”),因此现在还有一个 If 声明。
  • 第一个错误,第二个解决方案也给出了相同的结果
  • 那么您的原始代码中存在错误。例如:真的是第三列应该检查可能要上传的帐号吗?进入 VBE 并在Immediate 窗口中执行以下?Application.WorksheetFunction.CountA(Sheets("Cash and Bank Account Details").Listobjects("newaccount").ListColumns(3).DataBodyRange)。它会告诉您第 3 列中有多少个非空单元格。
猜你喜欢
  • 1970-01-01
  • 2014-08-31
  • 2018-03-26
  • 2011-10-19
  • 2016-05-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多