如何通过日期

问题描述 投票:0回答:1

我从这个关于如何添加日历的问题开始。 我已经运行了它,它几乎完全满足了我的需要。

我正在尝试修改它,以便它将日期传递到单元格,然后关闭。 我对VBA的了解有限。 即我不知道

#if
if
相比是什么。 我上次接受正式编程培训是在 1992 年左右,当时是 Turbo Pascal。我知道有全局变量和局部变量。 所以我试图找出将所选日期传递回单元格的正确方法。 我目前的做法是:

Sheet2“预测”代码

Private Sub StartDatePicker_Click()
    
Dim StartDate As Date

    CalendarModule.Launch (StartDate)
    Me.Cells(5, 2) = StartDate
End Sub

Private Sub EndDatePicker_Click()
        
Dim EndDate As Date
    CalendarModule.Launch (EndDate)
    Me.Cells(6, 2) = EndDate
End Sub

日历模块代码

Sub Launch(dDate As Date)
    Set f = frmCalendar
    
    With f
        .Caltheme = ArcticBlue
        .LongDateFormat = "dddd dd. mmmm yyyy" '"dddd mmmm dd, yyyy" etc
        .ShortDateFormat = "yyyy/mm/dd"  '"mm/dd/yyyy" or "d/m/y" etc
        .Show
    End With
End Sub

frm日历代码

'~~> Insert Selected date
Private Sub DTINSERT_Click()

Dim PickedDate as Date
    If Len(Trim(Label6.Caption)) = 0 Then
        MsgBox "Please select a date first", vbCritical, "No date selected"
        Exit Sub
    End If
    '~~> Change the code here to insert date where ever you want
    'PickedDate = Cdate(Label6.Caption)
    MsgBox Label6.Caption, vbInformation, "Date selected"
End Sub

问题

我对表单的使用完全不熟悉。 如何将 PickedDate 传递回 CalendarModule.Launch,然后返回 Sheet2.StartDatePicker_Click,然后再次运行它并将其返回到 Sheet2.EndDatPicker_Click

excel vba
1个回答
0
投票

您可以在frmCalendar中声明一个公共变量:

Public PickedDate as Date

然后

Sub Launch(dDate As Date)
    Set f = frmCalendar
    
    With f
        .Caltheme = ArcticBlue
        .LongDateFormat = "dddd dd. mmmm yyyy" '"dddd mmmm dd, yyyy" etc
        .ShortDateFormat = "yyyy/mm/dd"  '"mm/dd/yyyy" or "d/m/y" etc
        .Show
        Cells(5, 2) = f.PickedDate 
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.