userform框架中的Command按钮VBA excel

时间:2018-02-13 00:23:04

标签: excel vba excel-vba userform commandbutton

简言之:

  • 创建用户形式
  • 根据情况,使用文本框和按钮
  • 创建框架
  • 不确定我是否可以向动态创建的按钮添加代码。

更详细的解释: 我正在制作一款允许人们买卖股票的游戏,看看他们如何管理真实的投资组合。 该表由一个为玩家输入所有数据的人管理。

然而,我遇到了一个小问题。 在我的工作表上有一个“卖出”和“买入”按钮。

  1. 玩家可以从任意数量的股票开始。他们必须卖掉一个 股票,他们可以买一个。
  2. 当经理按下“购买”按钮时,会显示一个用户表单 用下拉列表选择他们想要做的球员 购买。
  3. 选择一个玩家后,代码会检查以确保 玩家在允许买入之前有卖出的位置。
  4. 如果一只股票已售出,我有一个框架就会出现 表格的下一部分要求股票代码 输入。它还有一个“提交”按钮,可以创建。
  5. 我的问题是,我不知道如何将代码添加到新的提交按钮。 通常情况下,这将是

    的内容
    Private Sub newbutton_click()
    

    但是我无法让它发挥作用。

    以下是细分的代码。

    首先,初始化userform:

    Option Explicit
    Option Compare Text
    
    Private Sub UserForm_initialize()
    Dim i As Integer, j As Integer
    Dim pn As String, s1 As String, s2 As String, s3 As String
    Dim name As Range, code As Range
    Dim lastrow As Integer
    Dim username As Range
    Dim names() As String, allnames() As String
    Dim fr1 As Integer, lr1 As Integer, lr2 As Integer
    Dim startv As String, curv As Integer, un, pr
    Dim count As Integer
    
    
    With ThisWorkbook.ActiveSheet.Range("A:Z")
        Set username = .find(What:="Players", after:=.Cells(.Cells.count), LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
        fr1 = username.Row + 1
        lr1 = .Cells(.Rows.count, username.Column).End(xlUp).Row
    
        ReDim Preserve names(fr1 To lr1)
        For i = fr1 To lr1
            If Cells(i, username.Column) <> "" Then
                names(i) = Cells(i, username.Column)
            End If
        Next i
    
        j = 0
        ReDim Preserve allnames(1 To UBound(names))
    
        For i = LBound(names) To UBound(names)
            If names(i) <> "" Then
                j = j + 1
                allnames(j) = names(i)
            End If
        Next i
    
        ReDim Preserve allnames(LBound(allnames) To j)
    
        For i = LBound(allnames) To UBound(allnames)
            Buy.namebox2.AddItem allnames(i)
        Next i
    
    End With
    End Sub
    

    此处的下一部分找到管理员选择的用户,并在允许购买之前检查库存是否已售出。

    Private Sub commandbutton1_click()
    Dim newLbl As MSForms.Label
    Dim newTxt As MSForms.Control
    Dim newTxt1 As MSForms.Control
    Dim newbut As MSForms.Control
    Dim i As Integer, TopAmt
    Dim fr1 As Integer, lr1 As Integer
    Dim j As Integer, lr As Integer
    Dim UserArray As Variant
    Dim start As Integer
    Dim username As Range, code As Range, selval As Range
    Dim firstrow As Integer, lastrow As Integer
    Dim names() As String, allnames() As String
    
    With ThisWorkbook.ActiveSheet.Range("A:Z")
        Set username = .find(What:=namebox2.value, after:=.Cells(.Cells.count), LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        Set code = .find(What:="Stock Code", after:=.Cells(.Cells.count), LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
        Set selval = .find(What:="Sell Value", after:=.Cells(.Cells.count), LookIn:=xlValues, _
                    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    
        If username Is Nothing Then
            MsgBox "Please select a player"
            Exit Sub
        End If
    
        fr1 = username.Row + 1
        lr1 = .Cells(.Rows.count, code.Column).End(xlUp).Row
    
        For i = fr1 To lr1
            If Cells(i, code.Column) = "TOTAL" Then
                lr = i
                Exit For
            End If
        Next i
    End With
    
    For i = fr1 To (lr - 1)
        If Cells(i, selval.Column) <> "" Then
            MsgBox "found a sold stock " & Cells(i, selval.Column)
            GoTo line1
        ElseIf i = (lr - 1) And Cells(i, selval.Column) = "" Then
            MsgBox "no stocks have been sold yet."
            Exit Sub
        End If
    Next i
    
    line1:
    
    TopAmt = 10
    
    Set newTxt = Frame1.Controls.Add(bstrProgID:="Forms.Label.1", name:="Label1")
            With newTxt
                .name = "Label1"
                .top = TopAmt
                .Caption = "Please enter the stock you wish to buy."
                .Visible = True
                .Width = 150
            End With
            TopAmt = TopAmt + newTxt.Height
    
    Set newTxt1 = Frame1.Controls.Add(bstrProgID:="Forms.Textbox.1", name:="Textbox1")
            With newTxt1
                .name = "Textbox1"
                .top = TopAmt
                .Visible = True
                .Width = 120
                .MaxLength = 3
            End With
            TopAmt = TopAmt + newTxt1.Height
    
    Set newbut = Frame1.Controls.Add(bstrProgID:="Forms.commandbutton.1", name:="commandbutton1")
            With newbut
                .name = "commandbutton1"
                .top = TopAmt
                .Visible = True
                .Width = 120
                .Caption = "Submit"
            End With
            TopAmt = TopAmt + newbut.Height
    
    End Sub
    

    所以我创建'newbut'并且需要在玩家点击它时添加代码。我只是不知道是否可能。

    提前感谢您,感谢任何意见。

0 个答案:

没有答案