【发布时间】:2018-07-10 17:49:53
【问题描述】:
我正在使用表单在 Excel 中填充工作表。在表单中,与日期相关的框由日-月-年组合框分解,这些组合框被连接起来填充工作表中的单个单元格。我的问题是是否有办法在单个单元格中“取消连接”日期并使用适当的信息(日、月或年)填充每个单独的组合框。这是因为我希望能够以一种形式添加新信息,然后以另一种形式更新表中已经存在的信息。
根据以下给定参数更新表单的代码(聚焦在粗体):
Private Sub txtstudynm_Change()
Dim StudyName As String
Dim WrdString As String
Dim text_string As String
If Me.txtstudynm.Value = "" Then
MsgBox "Study Name can not be blank", vbCritical
Exit Sub
End If
StudyName = txtstudynm.Value
On Error Resume Next
Me.cmbprojman.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 2, 0)
On Error Resume Next
Me.cmbstudtyp.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 3, 0)
On Error Resume Next
Me.cmbprogtyp.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 4, 0)
On Error Resume Next
Me.cmbfundtyp.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 5, 0)
On Error Resume Next
Me.txtbudget.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 6, 0)
On Error Resume Next
Me.txtencumb.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 7, 0)
On Error Resume Next
Me.cmbpath.Value = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 8, 0)
**On Error Resume Next**
**text_string = Application.WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 9, 0).Value**
**WrdString = Split(text_string, "/")(0)**
**Me.tssdcmb1.Value = WrdString**
On Error Resume Next
Me.tssdcmb2.Value = WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 9, 0)
On Error Resume Next
Me.tssdcmb3.Value = WorksheetFunction.VLookup(StudyName, Sheets("Study Summary").Range("A3:AJ3000"), 9, 0)
End Sub
【问题讨论】:
-
请注意,除非您使用
On Error Goto 0重置错误捕获,否则您只需要一个On Error Goto Next。它不是单行的,而是关闭错误报告,直到它被重置。 -
Sheets("Study Summary").Range("I:I")的格式是否为Date并包含 Excel 理解为Date值的值?或者它们是看起来像日期的String值? -
如果您的日期是日期,则传递的值不是文本而是数字,因此您可以使用
Month(text_string)返回月份数。 -
.Range("A3:AJ3000")是可以输入数据的工作表的整个区域。因此,日期将被格式化为日期的列,而其他列是通用的。另外,我尝试过使用Month()函数,但它似乎不起作用。 -
将所有三个答案组合成一个强大的解决方案后,请阅读@MathieuGuindon 的Userform.Show 并开始一些如何在为时已晚之前使用用户表单的好习惯。
标签: excel vba date combobox concatenation