Excel VBA用户表单-使用相同的表单生成连续数据

xbauks

我是VBA的新手。我想创建一个允许用户多次使用同一表格的用户表格。每次使用表单时,用户输入的任何数据都会添加到一个变量(相同变量或多个不同变量)中。当用户输入所有数据时,他们可以单击“提交”,并且表单将按顺序吐出所有数据。

示例:带有文本框和2个命令按钮的用户窗体,Next以及Submit用户输入1,单击Next用户输入2,单击Submit用户表格打印出来1, 2

我将如何去做呢?这有可能吗?

内维尔

这是一个快速的POC。考虑一个带有文本框和两个命令按钮的纯简用户格式。我根本没有更改默认名称。刚刚添加了一个带有textbox1,commandbutton1和commandbutton2的用户窗体:

在此处输入图片说明

CommandButton1提交并CommandButton2退出用户表单(例如,用户完成提交后单击)。

使代码按您描述的那样工作的代码。这在用户表单的代码页中。

'declare a string array to hold the submissions from textbox1
Private submissions() As String

'Code to run when the form activates
Private Sub UserForm_Activate()
    'When this userform is first initialized set up the array
    'as a one dimensional array with a single element
    ReDim submissions(0 To 0)
End Sub

'Code to run when the commandbutton1 is clicked
Private Sub CommandButton1_Click()
    'call the addSubmission sub
    addSubmission
End Sub

'Code to run when the commandButton2 is clicked
Private Sub CommandButton2_Click()

    'add submission one more time
    addSubmission

    'Now Loop through the array and send the values out
    'to the worksheet
    Dim rowCounter As Long
    rowCounter = 1
    For Each submission In submissions
        Sheet1.Cells(rowCounter, 1).Value = submission
        rowCounter = rowCounter + 1
    Next submission

    'Now close the form
    Me.Hide

    'And activate sheet1 for the user to see their submissions
    Sheet1.Activate
End Sub

'addSubmission will add textBox1 value to
'  the submissions array declared at the
'  top of this userform code.
Sub addSubmission()
    'First we have to redim the array to hold the new
    '   submission.
    'But only redim it if this isn't the first submission
    If UBound(submissions) > 0 Or submissions(0) <> "" Then ReDim Preserve submissions(0 To UBound(submissions) + 1)
    submissions(UBound(submissions)) = Me.TextBox1.Value

    'Clear the textbox so the user doesn't have to backspace
    Me.TextBox1.Value = ""
End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用 vlookup 的 Excel VBA 用户表单

使用excel vba验证用户表单数据

Excel VBA-每个标签/用户表单

Excel中从右到左的用户表单-VBA

在excel vba中使用用户表单添加IF规则

使用 Excel-VBA 更新用户表单的按钮

VBA Excel用户表单输出到Txt文件

VBA excel用户表单复选框问题

验证用户表单上的文本框条目(Excel VBA)

如何在Excel VBA中提高用户表单的保存速度

VBA用户表单中的MS Excel 2010 MAXIFS等效项

Excel VBA:从数组填充组合框(用户表单类型)

如何在Excel VBA中卸载多个用户表单?

Excel VBA 用户表单用按钮打开超链接

如何在VBA excel的用户表单中添加命令按钮?

Excel VBA +用户表单复选框::使用表单复选框隐藏/取消隐藏列

VBA-使用用户表单进行数据输入时Excel表跳过行

Excel VBA 用户表单图片到工作表单元格

将工作表数据插入到用户表单组合框中,没有重复 - Excel Vba

在excel VBA中选择另一个用户表单或空格时如何关闭用户表单?

如何使用集合为用户表单中的下拉列表提供选项 - Excel VBA

使用表单允许用户选择要运行的 excel Visual Basic (vba) 代码

VBA用户表单放置数据

Excel VBA 用户表单在服务器上使用用户名和密码登录

Excel:如何从VBA打开数据输入表单?

VBA Excel动态表单创建

将所有对象移回excel VBA用户表单中的原始位置

TypeOf 对象未检测到 Excel vba 用户表单中的标签和文本框

列表框在excel-vba用户表单中被设置为Null