首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Excel VBA用户窗体中访问控件

在Excel VBA用户窗体中访问控件
EN

Stack Overflow用户
提问于 2017-06-13 09:15:55
回答 2查看 171关注 0票数 0

我在一个自定义的用户表单中创建表单控件,在创建后访问它们有困难。ComboBoxes和TextBoxes的数量取决于用户输入。我正在使用一个CommandButton来确定正确的语法,但是我不知道该控件是什么。我在CommandButton_Click方法中使用了几种不同的命名约定,但都不起作用。

我的userform创建控件的代码如下:

代码语言:javascript
运行
复制
Sub createDetails()

Dim details As Variant


details = TextBox3.Value
remainTot = TextBox2.Value

If TextBox3.Value = "" Or TextBox3.Value = 0 Then
MsgBox "Must have at least 1 detail"
Exit Sub
Else
End If


For i = 1 To details

n = i - 1

Dim SubPay As Control
Dim CatPay As Control
Dim AmtPay As Control

Set theLbl = frmInvoice.Controls.Add("Forms.Label.1", "lbl_" & i, True)
    With theLbl
      .Caption = "Detail " & i
      .Left = 20
      .Width = 60
      .Top = n * 24 + 110
      .Font.Size = 10
    End With


Set SubPay = frmInvoice.Controls.Add("Forms.ComboBox.1", "SubComboBox_" & i, True)
    With SubPay
        .Top = 108 + (n * 24)
        .Left = 60
        .Height = 18
        .Width = 100
        .Name = "subBox" & i
        .Font.Size = 10
        .TabIndex = n * 3 + 6
        .TabStop = True
        .RowSource = "PayeeList"
    End With

Set CatPay = frmInvoice.Controls.Add("Forms.ComboBox.1", "CatComboBox_" & i, True)
    With CatPay
        .Top = 108 + (n * 24)
        .Left = 165
        .Height = 18
        .Width = 100
        .Name = "catBox" & i
        .Font.Size = 10
        .TabIndex = n * 3 + 7
        .TabStop = True
        .RowSource = "CatList"
    End With

Set AmtPay = frmInvoice.Controls.Add("Forms.TextBox.1", "AmtTextBox" & i, True)
    With AmtPay
        .Top = 108 + (n * 24)
        .Left = 270
        .Height = 18
        .Width = 50
        .Name = "amtBox" & i
        .Font.Size = 10
        .TabIndex = n * 3 + 8
        .TabStop = True

    End With

Next i

Dim TBox As Control

Set TBox = frmInvoice.Controls.Add("Forms.TextBox.1", "TotalLbl", True)
    With TBox
        .Top = 130 + ((details - 1) * 24)
        .Left = 270
        .Height = 18
        .Width = 50
        .Name = "totBox"
        .Font.Size = 10
        '.TabIndex = (details - 1) * 3 + 9
        .TabStop = False
        .Value = TextBox2.Value

    End With


Set theLbl = frmInvoice.Controls.Add("Forms.Label.1", "totLbl", True)
    With theLbl
      .Caption = "Total"
      .Left = 225
      .Width = 40
      .Top = 135 + ((details - 1) * 24)
      .Font.Size = 10
    End With

frmInvoice.Height = 200 + details * 24
With CommandButton1
.Top = 150 + details * 24
.TabStop = True
.TabIndex = (details - 1) * 3 + 9
End With

With CommandButton2
.Top = 150 + details * 24
.TabStop = False
'.TabIndex = (details - 1) * 3 + 10
End With



End Sub

不起作用的CommndButton的代码是:

代码语言:javascript
运行
复制
Private Sub CommandButton1_Click()

frmInvoice.Controls("amtBox1").Value = 1
frmInvoice.Controls(amtBox1).Value = 2
frmInvoice.Controls(AmtTextBox1).Value = 3
frmInvoice.Controls("AmtTextBox1").Value = 4

End Sub

任何帮助都是非常感谢的。

我的用户表单的屏幕截图:

EN

回答 2

Stack Overflow用户

发布于 2017-06-13 10:10:18

尝试使用

代码语言:javascript
运行
复制
frmInvoice.Controls("amtBox1").Text 

而不是

代码语言:javascript
运行
复制
frmInvoice.Controls("amtBox1").Value
票数 0
EN

Stack Overflow用户

发布于 2017-06-13 10:22:25

我认为你的错误是因为

代码语言:javascript
运行
复制
Private Sub CommandButton1_Click()

frmInvoice.Controls("amtBox1").Value = 1  'is true
frmInvoice.Controls(amtBox1).Value = 2    'is false because there isn't double quotes
frmInvoice.Controls(AmtTextBox1).Value = 3 'is false because  there isn't dobule quotes
frmInvoice.Controls("AmtTextBox1").Value = 4 'is false the AmtTextBoxt1 name is changed "amtBox1"

End Sub

玛比,你想这样吗?

代码语言:javascript
运行
复制
Private Sub CommandButton1_Click()
Dim i As Integer
Dim total As Currency
    With frmInvoice
        For i = 1 To TextBox3.Value
                total = total + .Controls("amtBox" & i).Value
        Next i
        .Controls("totBox").Text = Format(total, "####.00")
    End With

End Sub
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44510912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档