Need help displaying result of custom VBA function as label

muhes

New Member
Joined
Dec 12, 2007
Messages
1
Hello,

I am trying to display the result of a custom VBA function as a label on a userform. The function is called and takes arguments from three text boxes on the userform. I attached it to the exit parameter. (This may be part of the problem; I'm not sure).

There seem to be two problems, first all the text boxes don't always contain data, but because the arguments are not optional, I get a type mismatch error when the code runs (I tried switching some of them to optional, but it didn't help as you can see from my 'commented code).

The other problem is that even when all the arguments seem to be met (i.e. the text boxes all have data), I still get a compile error that the argument is not optional. This only happens if I try and pass the result to some aspect of the userform. As you can see from the last coded line in the custom function, if I assign it to a range on the worksheet, it functions fine.

Below are the codes. Any help you can provide would be appreciated. I use excel 03 and windows XP.

Note the exit procedure also contains a code that formats the text box to a specified date format, and this does work.

Code:
Private Sub txtStartDate_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim StartDate As Date
Dim EndDate As Date

    If IsDate(Me.txtStartDate.Value) Then
        StartDate = Me.txtStartDate.Value
        Me.txtStartDate.Value = Format(StartDate, "m/d/yyyy")
    Else:  MsgBox "Please enter a date"
    End If

Call CalcDate(txtStartDate, txtYears, txtMonths, txtDays)

EndDate = CalcDate

lblProjectPeriod.Caption = EndDate

End Sub

and the custom function:

Code:
Function CalcDate(StartDate As Date, Optional Years As Variant, Optional Months As Variant, _
    Optional Days As Variant) As Date
          
    'If IsMissing(Years) Then Years = False
       ' If Years = False Then Years = 0
       ' End If
    'End If
    'If IsMissing(Months) Then Months = False
       ' If Months = False Then Years = 0
     '   End If
   ' End If
   ' If IsMissing(Days) Then Days = False
       ' If Days = False Then Years = 0
       ' End If
    'End If
        
    CalcDate = DateAdd("yyyy", Years, StartDate)
    CalcDate = DateAdd("m", Months, CalcDate)
    CalcDate = DateAdd("d", Days, CalcDate)
    CalcDate = DateAdd("d", -1, CalcDate)
   
   'Range("L7") = CalcDate


Thanks for your help
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.

Forum statistics

Threads
1,214,979
Messages
6,122,561
Members
449,089
Latest member
Motoracer88

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