我的用户窗体中有两个TextBox。一个用于输入名称,另一个用于输入收入。
现在,我创建一个无模式的用户窗体,以便用户可以继续插入数据
假设我已经有姓名列表。例如:结婚,杰米,迈克尔
是否可以将此列表设置为Name TextBox的默认值? 例如:
点击按钮后,用户窗体弹出并显示如下:
Name: Marry
Income: ""
输入收入后,单击“确定”按钮,用户窗体将再次弹出。 这次显示如下:
Name: Jamie
Income: ""
如果我的问题不够清楚,请告诉我,我将更详细地解释。预先感谢。
更新:
我在此处添加代码以使问题更清楚。但是,我的代码的“故事”有点不同。用户将在用户表单中插入项目组合ID,预算值和日期。然后,宏将过滤工作表“ ALL_List”中的表格。
根据投资组合ID和日期,过滤后该表中将只有一行数据。此数据行的“预算”列应为空。宏应自动将在用户窗体中记录的预算值插入“预算”列。
例如,有5个ID和5个预算值:
日期/ ID /预算
29/06/2018 / 12541/336521
29/06/2018 / 4521/658882
29/06/2018 / 44359/4587996
29/06/2018 / 10223/148665
29/06/2018 / 74/658324
因此,当第一次使用用户窗体时弹出。我希望投资组合ID文本框中的默认ID值为“ 12541”。输入日期和预算值并单击按钮“ Enter”后,预算值将插入到工作表“ ALL_List”中的“预算”列。然后,用户窗体再次弹出。这次ID的默认值将是4521。
在显示最终的默认ID(74)之后,我输入值,然后单击Enter,我希望用户窗体仍会弹出,这次,Portfolio ID TextBox的值将为空(因为可能存在其他ID用户想要插入。)
希望我的描述清楚。如有任何疑问,请随时通知我。非常感谢!
Sub Budget_Adjustment()
Dim frm As New UserFormBudget
frm.Show vbModeless
End Sub
Private Sub ButtonClose_Click()
Unload Me
End Sub
Private Sub ButtonEnter_Click()
InsertBudget
End Sub
Private Sub InsertBudget()
Dim UpdateDate As String
Dim PortfolioID, Budgetvalue As Long
UpdateDate = TextBoxDate.Value
PortfolioID = TextBoxID.Value
Budgetvalue = TextBoxBedget.Value
UpdateDate = CDate(UpdateDate)
Sheets("ALL_List").Activate
ActiveSheet.AutoFilterMode = False
Range(Cells(1, 1), Cells(Cells(Rows.Count, 7).End(xlUp).row, 7)).AutoFilter Field:=1, Criteria1:=UpdateDate
Range(Cells(1, 1), Cells(Cells(Rows.Count, 7).End(xlUp).row, 7)).AutoFilter Field:=3, Criteria1:=PortfolioID
Cells(Cells(Rows.Count, "A").End(xlUp).row, "F").Value = Budgetvalue
ActiveSheet.AutoFilterMode = False
TextBoxID.Value = ""
TextBoxBedget.Value = ""
TextBoxID.SetFocus
End Sub
Private Sub TextBoxBedget_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
ButtonEnter_Click
End If
End Sub
Private Sub UserForm_Activate()
'Empty TextBoxID
TextBoxID.Value = ""
'Empty TextBoxBedget
TextBoxBedget.Value = ""
'Empty TextBoxDate
TextBoxDate.Value = ""
'Set Focus on NameTextBox
TextBoxDate.SetFocus
End Sub
答案 0 :(得分:1)
...
编辑:
根据您提供的新信息对代码进行了一些编辑。现在,您只需在名为“列表”的表中输入您要编辑的ID。
我添加了名为“列表”的工作表:
当您右键单击UserFormBudget>查看代码时,此代码将进入区域
Private Sub ButtonClose_Click()
Dim lastListRow As Long
With ThisWorkbook.Worksheets("List")
lastListRow = .Cells(.Rows.Count, 1).End(xlUp).row
.Range("A4:A" & lastListRow).Interior.ColorIndex = 0
End With
Unload Me
End Sub
Private Sub ButtonEnter_Click()
InsertBudget
End Sub
Private Sub InsertBudget()
Dim UpdateDate As String
Dim PortfolioID As Long
Dim Budgetvalue As Long
Dim lastListRow As Long
Dim row As Long
UpdateDate = TextBoxDate.Value
PortfolioID = TextBoxID.Value
Budgetvalue = TextBoxBedget.Value
If Len(UpdateDate) > 0 Then
UpdateDate = CDate(UpdateDate)
Else
MsgBox "Need to enter a date"
Exit Sub
End If
With Worksheets("ALL_List")
.Activate
.AutoFilterMode = False
.Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 7).End(xlUp).row, 7)).AutoFilter Field:=1, Criteria1:=UpdateDate
.Range(.Cells(1, 1), .Cells(.Cells(.Rows.Count, 7).End(xlUp).row, 7)).AutoFilter Field:=3, Criteria1:=PortfolioID
.Cells(.Cells(.Rows.Count, "A").End(xlUp).row, "F").Value = Budgetvalue
.AutoFilterMode = False
End With
With ThisWorkbook.Worksheets("List")
lastListRow = .Cells(.Rows.Count, 1).End(xlUp).row
TextBoxID.Value = ""
For row = 5 To lastListRow
If .Cells(row, "A").Interior.Color <> RGB(255, 255, 0) Then
TextBoxID.Value = .Cells(row, "A").Value
.Cells(row, "A").Interior.Color = RGB(255, 255, 0)
Exit For
End If
If row = lastListRow Then
TextBoxDate.Value = ""
End If
Next
End With
TextBoxBedget.Value = ""
TextBoxID.SetFocus
End Sub
Private Sub TextBoxBedget_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
If KeyCode = 13 Then
ButtonEnter_Click
End If
End Sub
并将此代码添加到模块中,因此右键单击项目并插入新模块,然后粘贴:
Sub Budget_Adjustment()
Dim frm As New UserFormBudget
Dim lastListRow As Long
With ThisWorkbook.Worksheets("List")
lastListRow = .Cells(.Rows.Count, 1).End(xlUp).row
If lastListRow = 3 Then
frm.TextBoxDate.Value = ""
frm.TextBoxID.Value = ""
frm.TextBoxBedget.Value = ""
Else
frm.TextBoxID.Value = .Cells(4, "A").Value
frm.TextBoxBedget.Value = .Cells(4, "B").Value
.Cells(4, "A").Interior.Color = RGB(255, 255, 0)
End If
End With
frm.TextBoxID.SetFocus
frm.Show vbModeless
End Sub
现在,只需右键单击“列表”表上的按钮,然后为其分配宏Budget_Adjustment