Returning Date from Calendar

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,907
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am trying to return a date using Calendar Control.

I have Calendar1 within frmCalendar.

Code for Calendar1
Code:
Private Sub Calendar1_Click()
    Dim iDate
            iDate = Calendar1.Value
    Unload Me
End Sub

I then have a command button located within worksheets to run another macro. The macro that it calls is call_data:
Code:
Sub call_data()

Dim ws As Worksheet
Dim lRow, fRow, Lines As Long
Dim iDate As Date

Application.ScreenUpdating = False

Set ws = ActiveSheet
lRow = Cells(Rows.Count, 2).End(xlUp).Row + 1

cmdDatabaseOpen_Click

Lines = Cells(Rows.Count, 1).End(xlUp).Row - 1

Cells(2, 1).Resize(Lines, 2).Copy _
    ws.Cells(lRow, 2)

ActiveWorkbook.Close

frmCalendar.Show

iDate = Calendar1.Value

fRow = lRow
lRow = fRow + Lines - 1

For i = fRow To lRow
    On Error Resume Next
        Cells(i, 5) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 8, 0)
        Cells(i, 6) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 3, 0)
        Cells(i, 7) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 10, 0)
        Cells(i, 8) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 6, 0)
        Cells(i, 9) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 13, 0)
        Cells(i, 11) = iDate
        Cells(i, 13) = Application.WorksheetFunction.SumIf(Range("I5:I" & i), Range("I" & i), Range("C5:C" & i))
        With Cells(i, 15)
            .Formula = "=SUMPRODUCT(--(M" & i & ">" & ws.Name & "_Bands),(M" & i & "-" & ws.Name & "_Bands)," & ws.Name & "_Diff)"
            .Value = .Value
        End With
        Cells(i, 14) = Cells(i, 15) - Application.WorksheetFunction.SumIf(Range("I5:I" & i - 1), Range("I" & i), Range("N5:N" & i - 1))
Next

Application.ScreenUpdating = True

End Sub

How do I link iDate in Calendar1 to iDate in call_data?

Best regards
Jon
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I would probably send the calendar date to a cell somewhere on the worksheet so that you can get it back whenever you want, i.e:
Code:
Range(“A1”) = Calendar1.Value


iDate = Range(“A1”)
 
Upvote 0
Dim iDate only once at the top of a General module. Then it will be available to all your procedures. Don't forget to remove any procedure-level declarations.
 
Upvote 0
Hi Andrew

Thanks for reply. :)

I have placed Dim iDate as Date above general module as stand-alone line of code.

Then removed Dim iDate from call_data and from Calendar1_Click.
Code:
Private Sub Calendar1_Click()
    iDate = Calendar1.Value
    Unload Me
End Sub

But when I run call_data it returns zero. Yet it will return the correct date if passed to the active cell.

Any idea what I'm doing wrong? :confused:

Edit:
Don't forget to remove any procedure-level declarations.
How do I do this?

Thanks
Jon
 
Upvote 0
In:

Code:
Sub call_data() 

Dim ws As Worksheet 
Dim lRow, fRow, Lines As Long 
Dim iDate As Date

Did you remember to remove:

Code:
Dim iDate As Date
 
Upvote 0
Hi Andrew

Yes I did but it still returns zero value.
Code:
Sub call_data()

Dim ws As Worksheet
Dim lRow, fRow, Lines As Long

Application.ScreenUpdating = False

Set ws = ActiveSheet
lRow = Cells(Rows.Count, 2).End(xlUp).Row + 1

cmdDatabaseOpen_Click

Lines = Cells(Rows.Count, 1).End(xlUp).Row - 1

Cells(2, 1).Resize(Lines, 2).Copy ws.Cells(lRow, 2)

ActiveWorkbook.Close

fRow = lRow
lRow = fRow + Lines - 1

frmCalendar.Show

For i = fRow To lRow
    On Error Resume Next
        Cells(i, 5) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 8, 0)
        Cells(i, 6) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 3, 0)
        Cells(i, 7) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 10, 0)
        Cells(i, 8) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 6, 0)
        Cells(i, 9) = Application.WorksheetFunction.VLookup(Cells(i, 2), Range("PC_Map"), 13, 0)
        Cells(i, 11) = iDate
        Cells(i, 13) = Application.WorksheetFunction.SumIf(Range("I5:I" & i), Range("I" & i), Range("C5:C" & i))
        With Cells(i, 15)
            .Formula = "=SUMPRODUCT(--(M" & i & ">" & ws.Name & "_Bands),(M" & i & "-" & ws.Name & "_Bands)," & ws.Name & "_Diff)"
            .Value = .Value
        End With
        Cells(i, 14) = Cells(i, 15) - Application.WorksheetFunction.SumIf(Range("I5:I" & i - 1), Range("I" & i), Range("N5:N" & i - 1))
Next

Application.ScreenUpdating = True

End Sub

And Private Sub Calendar1_Click() remains as per my last post.

Thanks
Jon
 
Upvote 0
If I change the Calendar1_Click to populate the active cell then it returns the date selected, but as long as I use iDate it returns zero, and as such all cells within range fRow through lRow are zero.
Code:
Private Sub Calendar1_Click() 
    ActiveCell = Calendar1.Value 
    Unload Me 
End Sub
 
Upvote 0
This worked for me:

Code:
'General module
Public iDate As Date

Sub call_data()
    frmCalendar.Show
    MsgBox iDate
End Sub

'UserForm module
Private Sub Calendar1_Click()
    iDate = Calendar1.Value
    Unload Me
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,338
Messages
6,124,356
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top