如果没有范围加载到组合框中,则限制用户窗体加载的代码 [英] Code to restrict userform from loading if there is no range to be loaded in combobox

查看:96
本文介绍了如果没有范围加载到组合框中,则限制用户窗体加载的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个小的用户窗体,其中包含一个组合框。加载用户窗体时,此组合框将填充工作表中的范围。我需要的是,如果没有值加载到组合框中,它应该显示一个错误没有任何加载。

i have developed a small userform with one combobox in it. This combobox is filled with the range in worksheet upon loading the userform. What i need is, if there is no value to load in combobox, it should display an error "Nothing is there to load". This userform is loaded through a small button.

以下是我的用户窗体初始化代码:

Below is my userform initialization code:

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

请仔细阅读并帮助我查询。

Kindly review and help me with the query.

谢谢。

推荐答案

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

I am guessing that you are looking for something like this:

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

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

Just before showing the form to the user you can check if there is anything in the ComboBox1 worth showing.

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

Alternatively, you can even check before initializing the form:

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

这篇关于如果没有范围加载到组合框中,则限制用户窗体加载的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆