Excel VBA CDate Type Mismatch Error

Biz

Well-known Member
Joined
May 18, 2009
Messages
1,773
Office Version
  1. 2021
Platform
  1. Windows
Dear All,

I have compile a code it works well except when Text is evaluated, which are not dates like "Weekly" and "MTD".
Line in bold red gives type mismatch error. Your help would be greatly appreciated.


Code:
Sub Add_DataFields(pt As PivotTable, rng As Range)    Dim ptField As PivotField, ptItem As PivotItem
    Dim vPTDataFields, sField
    Dim i As Long
    
    '~~~> Convert 1D to 2D
    vPTDataFields = Application.Transpose(rng.Value)
    
    '~~~> Loop Thru Data Fields
    For i = LBound(vPTDataFields) To UBound(vPTDataFields)
        
        sField = [COLOR=#FF0000][B]IIf(Not (IsError((CDate((vPTDataFields(i, 1)))))), Format(vPTDataFields(i, 1), "ddd dd/mm/yyyy"), vPTDataFields(i, 1))[/B][/COLOR]
        'MsgBox IIf(Application.IsNumber(vPTDataFields(i, 1)), Format(vPTDataFields(i, 1), "ddd mmm yyyy"), vPTDataFields(i, 1)), vbInformation
        Debug.Print "For array element " & i & " the number is " & vPTDataFields(i, 1)
       ' pt.AddDataField pt.PivotFields("Thu 24/11/2016"), "Sum of Thu 24/11/2016", xlSum
        pt.AddDataField pt.PivotFields(sField), "Sum of " & sField, xlSum
    Next i
    
    Set pt = Nothing
    
End Sub


Biz:oops:
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Hi Mate,

Conversion functions will generate a runtime error even if they are wrapped in an IsError() function.

Have you tried testing your non-date field names with this...
sField = Format(vPTDataFields(i, 1), "ddd dd/mm/yyyy")

That Format expression will return "MTD" and "Weekly" from those inputs.

If that doesn't work and if you know that your valid dates are of a format that will return True when evaluated by IsDate(), then you could use this...
Code:
sField = IIf(IsDate(vPTDataFields(i, 1)),....
That would work for input expressions like "12/10/2016", but not for serial values like 42714.

If you need to handle serial values, then try calling a function that has an error handler.
Code:
Function bIsDate(vInput As Variant) As Boolean
 On Error Resume Next
 bIsDate = Not IsError(CDate(vInput))
End Function

Function call...
Code:
sField = IIf(bIsDate(vPTDataFields(i, 1)),....
 
Last edited:
Upvote 0
Hi Jerry,

The UDF worked like a charm. Thank you for your help.
Merry Xmas and Happy New Year.

Kind Regards

Biz
 
Upvote 0

Forum statistics

Threads
1,214,891
Messages
6,122,105
Members
449,066
Latest member
Andyg666

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