简体   繁体   English

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

[英]Accesing Controls in Excel VBA Userform

I am creating form controls in a custom userform and have having difficulty in accesing them after creation. 我在自定义用户窗体中创建窗体控件,创建后难以访问它们。 The amount of ComboBoxes and TextBoxes depends on user input. ComboBoxes和TextBoxes的数量取决于用户输入。 I am using a CommandButton to figure the correct syntax, but am at a loss to find the control. 我使用CommandButton来确定正确的语法,但是不知所措。 I have used several different naming conventions inside the CommandButton_Click method and nothing works. 我在CommandButton_Click方法中使用了几种不同的命名约定,但没有任何效果。

My code for the userform to create my controls is as follows: 我用于创建控件的用户窗体的代码如下:

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

The code for the CommndButton which doesn't work is: CommndButton无效的代码是:

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

Any help is greatly appreciated. 任何帮助是极大的赞赏。

Screen shot of my userform: 我的用户窗体的屏幕截图:

我的用户表格

Try using 尝试使用

frmInvoice.Controls("amtBox1").Text 

instead of 代替

frmInvoice.Controls("amtBox1").Value

I think your error because of 我认为您的错误是由于

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

在此处输入图片说明

Maby, Do you want to this? Maby,您要吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM