VBA userform - 动态文本框默认值

VBA userform - Dynamic textbox default value

我的用户窗体中有两个文本框。一个是输入姓名,一个是输入收入。

现在我创建了一个无模式的用户窗体,这样用户就可以继续插入数据

假设我已经有了姓名列表。例如:玛丽、杰米、迈克尔

是否可以将此列表设置为名称文本框的默认值? 例如:

点击按钮后弹出用户窗体显示如下:

Name: Marry
Income: ""

输入收入后,点击"OK"按钮,用户表单又会弹出。 这次是这样显示的:

Name: Jamie
Income: ""

如果我的问题不够清楚,请告诉我,我会解释得更详细。提前致谢。


更新:

我在这里添加我的代码以使我的问题更清楚。但是,我的代码的 "story" 有点不同。用户将投资组合 ID、预算值和日期插入用户表单。然后宏会过滤table in sheet "ALL_List".

根据Portfolio ID和日期,筛选后table中只有一行数据。此数据行的预算列应为空。宏应自动将用户窗体中记录的预算值插入预算列。

例如有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

所以,当userform第一次弹出的时候。我希望 Portfolio ID TextBox 中会有一个默认 ID 值“12541”。在我输入日期和预算值并单击按钮 "Enter" 后,预算值将插入到 sheet "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

...

编辑:

根据您提供的新信息稍微编辑了您的代码。现在,您只需在名为 "list".

的 sheet 中预先输入您要编辑的 ID

我添加了 sheet 命名为 "List" :

当您右键单击 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

现在,只需右键单击列表 sheet 上的按钮并为其分配宏 Budget_Adjustment