简言之:
更详细的解释: 我正在制作一款允许人们买卖股票的游戏,看看他们如何管理真实的投资组合。 该表由一个为玩家输入所有数据的人管理。
然而,我遇到了一个小问题。 在我的工作表上有一个“卖出”和“买入”按钮。我的问题是,我不知道如何将代码添加到新的提交按钮。 通常情况下,这将是
的内容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'并且需要在玩家点击它时添加代码。我只是不知道是否可能。
提前感谢您,感谢任何意见。