如何通过VBA中的循环动态添加用户表单复选框? [英] How to add check boxes to a user form dynamically through a loop in VBA?

查看:460
本文介绍了如何通过VBA中的循环动态添加用户表单复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过代码

动态地将复选框添加到UserForms

我试图在用户表单的多个复选框中收集特定文件夹中的所有文件名,以便我可以选择一个移动到另一个文件夹....添加一个复选框后,我无法在运行时添加另一个请帮助。






  Private Sub Download_file_Click()
Dim wrkbk As Workbook
Dim ass As New FileSystemObject
Dim ofolder As Folder
Dim作为文件
Dim mars_path As String
Dim chkBox As MSForms.CheckBox
Dim files_count As Integer
Dim i As Integer
Dim sheet_name As String
Dim NewCheckBox As MSForms.CheckBox
Dim file_name As String

设置wrkbk = Application.Workbooks(Download files.xlsm)


mars_path = input_path .Value
如果mars_path =然后
MsgBox请先提供输入路径!
退出Sub
结束If
如果ofs.FolderExists(mars_path)然后
Set ofolder = ofs.GetFolder(mars_path)
MsgBoxfiles are present !!
End If

files_count = ofolder.Files.Count

sub_folder_count = ofolder.SubFolders.Count
MsgBox#files =& files_count& 和#folder =& sub_folder_count


'********文件夹检查***************
i = 1

'创建用户表单
设置myform = wrkbk.VBProject.VBComponents.Add(3)
使用myform
.Properties(caption)=新窗体
结束

对于每一个inolder.Files
file_name = ofile.Name
MsgBox文件名

'创建复选框

设置NewCheckBox = myform.designer.Controls.Add(Forms.CheckBox。& i)

使用NewCheckBox
.Caption = file_name
结束
= b



b'创建CheckBox
设置NewCheckBox = myform.designer.Controls.Add(Forms.CheckBox。& i)
使用NewCheckBox
.Caption = file_name
结束
i = i + 1
Next SubFolders

卸载我
End Sub
pre>

解决方案

尝试这个

  Set NewCheckBox = myform.Controls.Add(Forms.CheckBox.1,Check&我,真的)'第一个字符串必须是这个,第二个是名字,第三个是可见=真。 

您仍然可能需要将新的控件添加到数组,集合或字典(我的首选,因为更容易删除内容并测试是否存在)。



希望这有帮助。
Patrick Lepelletier。



编辑:通过在VBE上按F1,在Userform1.controls.add,或此链接 https://msdn.microsoft.com/en-us/library/office/gg251373.aspx


Add Check Box to UserForms Dynamically through code

I am trying to collect all file names in a specific folder in multiple check boxes in a user form so that i can select one by one to move to another folder....after adding one check box i couldn't add another during run time please help.


Private Sub Download_file_Click()
Dim wrkbk As Workbook
Dim ofs As New FileSystemObject
Dim ofolder As Folder
Dim ofile As File
Dim mars_path As String
Dim chkBox As MSForms.CheckBox
Dim files_count As Integer
Dim i As Integer
Dim sheet_name As String
Dim NewCheckBox As MSForms.CheckBox
Dim file_name As String

Set wrkbk = Application.Workbooks("Download files.xlsm")


mars_path = input_path.Value
    If mars_path = "" Then
        MsgBox "Please provide input path first!"
        Exit Sub
    End If
If ofs.FolderExists(mars_path) Then
        Set ofolder = ofs.GetFolder(mars_path)
        MsgBox "files are present!!"
End If

files_count = ofolder.Files.Count

sub_folder_count = ofolder.SubFolders.Count
MsgBox "# files =" & files_count & "and # folder =" & sub_folder_count


'********folder check***************
    i = 1

            'Create the User Form
            Set myform = wrkbk.VBProject.VBComponents.Add(3)
            With myform
            .Properties("caption") = "New Form"
            End With

    For Each ofile In ofolder.Files
        file_name = ofile.Name
        MsgBox file_name

        'Create CheckBox

        Set NewCheckBox = myform.designer.Controls.Add("Forms.CheckBox."&i)

        With NewCheckBox
            .Caption = file_name
        End With
    i = i + 1
   Next ofile
i = 1
    For Each SubFolders In ofolder.SubFolders
        file_name = SubFolders.Name
        MsgBox file_name

        'Create CheckBox
        Set NewCheckBox = myform.designer.Controls.Add("Forms.CheckBox." & i)
        With NewCheckBox
            .Caption = file_name
        End With
    i = i + 1
    Next SubFolders

Unload Me
End Sub  

解决方案

try this

 Set NewCheckBox = myform.Controls.Add("Forms.CheckBox.1", "Check" & i, true) ' first string must be this, 2nd is name, 3rd is visible=true.

you still might want to add the new controls to an array, collection, or dictionary (my prefered, because easier to remove stuff and test if exists).

Hope this helps. Patrick Lepelletier.

edit: "more" help by pressing F1 in VBE on the line Userform1.controls.add , or this link https://msdn.microsoft.com/en-us/library/office/gg251373.aspx

这篇关于如何通过VBA中的循环动态添加用户表单复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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