【发布时间】:2015-09-02 01:44:51
【问题描述】:
我在 Excel 中创建了一个 UserForm1 并将其保存为加载项。此加载项工作正常,但它不存储我需要的一些数据(不将其存储在本身而不是在打开的 excel 中)。我必须在单元格 A1 和 A2 中存储一些信息(在 A1 用户名中,在 A2 今天的日期中)。
当我运行这个加载项时,UserForm1 不包含这些值。
有什么方法可以存储用户名并获取更新日期?
这是 UserForm1 的代码:
Private Sub UserForm1_Initialize()
Me.DocumentName.Text = ActiveWorkbook.FullName
DocumentName.Visible = False
TextBoxDate.Value = Worksheets("Sheet1").Cells(2, "A").Value
TextBoxDate.Value = CDate(TextBoxDate.Value)
UserName.Visible = False
Userform1.UserName.Text = CStr(Range("A1").Value)
'If A1 is empty pops up a UserRegister form
If UserName = "" Then
UserRegister.Show
End If
End Sub
用户注册表格代码:
Private Sub UserName_Change()
Sheets("Sheet1").Range("A1") = UserName.Text
End Sub
' I want to store the UserName, so the user does not have to enter it every single time
Private Sub CommandButtonGO_Click()
ThisWorkbook.Save
Unload Me
End Sub
要获取日期,我只需在单元格 A2 中使用公式 =TODAY()。我知道还有其他方法,但我发现这个方法很简单。
【问题讨论】:
-
使用
ThisWorkbook.Worksheets("Sheet1").Cells(2, "A").Value、ThisWorkbook.Worksheets("Sheet1").Range("A1").Value和ThisWorkbook.Sheets("Sheet1").Range("A1").Value。如果要存储用户名更改日期,则应删除公式并将值设置在Private Sub UserName_Change()
标签: vba excel userform office-addins