簡體   English   中英

在Excel VBA用戶窗體中訪問控件

[英]Accesing Controls in Excel VBA Userform

我在自定義用戶窗體中創建窗體控件,創建后難以訪問它們。 ComboBoxes和TextBoxes的數量取決於用戶輸入。 我使用CommandButton來確定正確的語法,但是不知所措。 我在CommandButton_Click方法中使用了幾種不同的命名約定,但沒有任何效果。

我用於創建控件的用戶窗體的代碼如下:

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無效的代碼是:

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

任何幫助是極大的贊賞。

我的用戶窗體的屏幕截圖:

我的用戶表格

嘗試使用

frmInvoice.Controls("amtBox1").Text 

代替

frmInvoice.Controls("amtBox1").Value

我認為您的錯誤是由於

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,您要嗎?

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