Novice needs help using excel date function in vba on a userform

PKiss

New Member
Joined
Sep 22, 2011
Messages
34
Hello,
I have found myself in a confusing syntax situation. The code included shows how i desire to use the application.workbookfunction.date().
It produces different errors as i tinker with it leading me to ask for insight from someone more knowledgeable then myself. (I having only a few days experience with VBA)

This code produces this error at "cdate1="
run-time error '438':
Object doesnt support this property or method.

Suggestions?
Thanks in advance for your help and im sorry if this topic was answered before. (i couldnt find a solution)

Code:
Private Sub UserForm_Initialize()
Dim cmonth  As Range
Dim cday As Range
Dim ws As Worksheet

Set ws = ThisWorkbook.Sheets("Sheet2")

For Each cmonth In ws.Range("Month")
    With Me.month1
        .AddItem cmonth.Value
    End With
Next cmonth

For Each cday In ws.Range("Day")
    With Me.day1
    .AddItem cday.Value
    End With
Next cday

For Each cmonth In ws.Range("Month")
    With Me.month2
    .AddItem cmonth.Value
    End With
Next cmonth

For Each cday In ws.Range("Day")
    With Me.day2
    .AddItem cday.Value
    End With
Next cday

Me.month1 = "January"
Me.day1 = 1
Me.year1 = 2000
Me.month2 = "January"
Me.day2 = 2
Me.year2 = 2000

End Sub
Private Sub compute_Click()
'Dim cdate1 As Double
'Dim cdate2 As Double
'Dim cans As Double

[COLOR="Red"]cdate1 = Application.workbookfunction.Date(Me.year1, Me.month1, Me.day1)
cdate2 = Application.workbookfunction.Date(Me.year2, Me.month2, Me.day2)[/COLOR]

If Me.weekend.Value = False Then

cans = cdate2 - cdate1
Else: Me.weekend.Value = True
ans = workbookfunction.NetworkDays(cdate1, cdate2)
Me.result.Value = cans
End If
End Sub
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I would use the VBA function DateSerial instead of the WorksheetFunction.Date.
Especially because there is no WorksheetFunction.Date.

The reason that there isn't is that when there is a VBA function that does the same thing as a Worksheet function, that worksheet function is not included in the Application.WorksheetFunction collection.
 
Upvote 0
Thanks mikerickson.
I was beginning to suspect that since google wasn't getting any hits on it. I just needed to know as a fact rather then a suspicion. Thanks again.
 
Last edited:
Upvote 0
If you enter WorksheetFunction in the Object Browser, it will bring up a list of all of them.

The Object Browser is your friend. Your very good friend.
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,304
Members
452,904
Latest member
CodeMasterX

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