VBA用户表单,如何存储先前输入的值以供下次打开

时间:2019-01-25 16:16:50

标签: excel vba

因此,我有一个用户表单,用户可以在其中输入数据,一些在组合框中,而在文本框中。然后将此数据填充到工作表中。下一次打开数据时,使用户重新显示该数据的最佳方法是什么?因此,如果用户在“型号/推出年份”字段中输入“ 2014”,然后点击“提交”并关闭“开始日期”,那么下次他们打开该字段时,我希望该字段仍说“ 2014”。代码在下面并且可以正常工作,作为IM和VBA的新手,它可能是混乱且效率低下的,我更关心的是使此功能在此处工作而不是优化我的代码。谢谢!

enter image description here

'=========================================================Populating User Form Drop Downs============================================================
Private Sub UserForm_Initialize()

'populate OEM drop down
With Me.ComboBox1
    .AddItem "GM"
    .AddItem "Ford"
    .AddItem "FCA"
    .AddItem "Toyota"
    .AddItem "Nissan"
    .AddItem "Isuzu"
    .AddItem "Other"
End With

'populate Program stage dropdown
With Me.ComboBox4
    .AddItem "Screen Approval"
    .AddItem "Quote Approval"
    .AddItem "Program Start"
    .AddItem "Design Verification Release"
    .AddItem "Production Validation Release"
    .AddItem "Production Part Approval Process"
    .AddItem "Start of Production"
    .AddItem "Program Closure"
End With

'Populate Milestones

Dim M1 As Variant
M1 = Sheets("Monthly Status").Range("G23")
M13 = Sheets("Monthly Status").Range("G35")
M14 = Sheets("Monthly Status").Range("G36")
M15 = Sheets("Monthly Status").Range("G37")
M16 = Sheets("Monthly Status").Range("G38")
M17 = Sheets("Monthly Status").Range("G39")
M24 = Sheets("Monthly Status").Range("G46")
M25 = Sheets("Monthly Status").Range("G47")
M26 = Sheets("Monthly Status").Range("G48")
M37 = Sheets("Monthly Status").Range("G59")

'Populate OP drop down

With Me.ComboBox2
    .AddItem M1
    .AddItem M13
    .AddItem M14
    .AddItem M15
    .AddItem M16
    .AddItem M17
    .AddItem M24
    .AddItem M25
    .AddItem M26
    .AddItem M37
End With

End Sub
Private Sub Submit_Click()

'======================================================Filling Header Feilds From User Form==========================================================

Dim OEM As String
Dim Var As String
Dim Year As String
Dim Veh As String
Dim VehC As String
Dim DRE As String
Dim ID As String
Dim Unit As String
Dim AAE As String
Dim DTE As String
Dim Stage As String

'Fill OEM box
    OEM = ComboBox1.value
    Sheets("Monthly Status").Range("E7:G7") = OEM

'Fill Variant(s) box
    Var = TextBox2.value
    Sheets("Monthly Status").Range("E8:G8") = Var

'Fill Model / Launch Year
    Year = TextBox3.value
    Sheets("Monthly Status").Range("I7") = Year

'Fill Vehicle(s) Box
    Veh = TextBox4.value
    Sheets("Monthly Status").Range("I8:L8") = Veh

'Fill Vehicle Code(s) Box
    VehC = TextBox5.value
    Sheets("Monthly Status").Range("L7:O7") = VehC

'Fill Acoustics DRE Box
    DRE = TextBox6.value
    Sheets("Monthly Status").Range("R7:S7") = DRE

'Fill Program ID Box
    ID = TextBox7.value
    Sheets("Monthly Status").Range("R8:S8") = ID

'Fill Reporting Region/Business Unit
    Unit = TextBox8.value
    Sheets("Monthly Status").Range("R12") = Unit

'Fill Assigned AAE
    AAE = TextBox9.value
    Sheets("Monthly Status").Range("R13") = AAE

'Fill Assigned DTE
    DTE = TextBox10.value
    Sheets("Monthly Status").Range("R14") = DTE

'Full Current TenPLUS Program Stage
    Stage = ComboBox4.value
    Sheets("Monthly Status").Range("R16") = Stage

End Sub

2 个答案:

答案 0 :(得分:1)

使用Me.Hide代替Unload Me,下一次您.Show用户窗体时,它仍将包含相同的值。

但是:当您关闭并重新打开工作簿时,这些值将会丢失

答案 1 :(得分:1)

在初始化期间,您将加载先前保存的值,例如

TextBox1.Text = "Selected Vehicle"
...
Combobox1.ListIndex = ComboBox1_SelectionInt
Combobox2.ListIndex = ComboBox2_SelectionInt
...

您可能要使用上面提到的Me.hide提案。

在我卸载过程中或关闭工作簿时,您要保存的表单值例如到一个隐藏的工作表(例如具有命名范围)

With Workheets("HiddenSheetForUserform")
    .Range("Textbox1") := "Textbox1 value"
    .Range("Textbox2") := "Textbox2 value"
    .Range("Textbox3") := "Textbox3 value"
    '...

    .Range("Combobox1") := ComboBox1_SelectionInt
    .Range("Combobox2") := ComboBox2_SelectionInt
End With