重复使用UserForm VBA代码在同一工作表中的多个dateselect器

我想知道如何重用VBA代码,我必须在同一个工作表上input不同的单元格来创build多个日历dateselect器。 我已经尝试更改.frm文件的名称,并重新导入它以简单地更改单元格引用输出,但是Excel每次都会拒绝此操作,并说名称已被使用。 我正在密切关注这个例子 。

如果您知道如何隐藏可点击图片的代码,直到选中了具有date的单元格,则奖励点数。

以下是我目前在frmCalendar中使用的代码:

Private Sub cmdClose_Click() Unload Me End Sub Private Sub MonthView1_DateClick(ByVal DateClicked As Date) On Error Resume Next Dim cell As Object For Each cell In Range("C18") cell.Value = DateClicked Next cell Unload Me End Sub Private Sub UserForm_Initialize() If IsDate(ActiveCell.Value) Then Me.MonthView1.Value = Range("C18") End If End Sub 

…和我的代码为Module1:

 Sub Sample() frmCalendar.Show End Sub 

所以基本上,我有一个日历,卸载到C18。 不过,我想在工作表中有最多10个日历button,它们有不同的输出单元,所以它们可以有不同的date。

以下是日历button输出到C18的示例。 它已被分配macros“样本”

在这里输入图像说明

那么我怎样才能重复使用我的代码多个日历button? 奖励,如果有代码隐藏日历button,直到选中单元格。

您可以在窗体打开时存储选定的单元格,并在closures时使用该单元格。

 Dim cell As Range 'selected cell when launched Private Sub cmdClose_Click() Unload Me End Sub Private Sub MonthView1_DateClick(ByVal DateClicked As Date) On Error Resume Next cell.Value = DateClicked Unload Me End Sub Private Sub UserForm_Initialize() Set cell = Selection.Cells(1) If IsDate(cell.Value) Then Me.MonthView1.Value = cell.Value End If End Sub