Help with VBA 'Type Mismatch' error & date code

  • Hi, I need help with a date code that gives 'Type Mismatch' error and I would also like to modify the date code to exclude the current and previous month and only include all months prior. See notes in the code attached.

    Files

    • Oz Date.xlsm

      (17.83 kB, downloaded 38 times, last: )
  • The reason for the Type Mismatch Error is because of the values (1 to 10) that you have added to Row 5, they are part of the [a6].CurrentRegion and so get included when loading array x. The first row of array x should be the Header Row and the loop start at the second row of the array, but with those extra values they form the first row of the array and the 2nd row is the header row, the code then tries to get the Year of a header which is not a date hence the error.


    To correct it you have 3 options

    1. Remove the values in Row 5;
    2. Move those values to Row 4 (making Row 5 empty
    3. Change the code as shown below

    Change

    Code
    1. For i = 2 To UBound(x, 1)           

    To

    Code
    1. For i = 3 To UBound(x, 1)

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Note that even with 1 of the 3 options being used your code will not produce a result with your current test data, it is now Month 9 and the maximum Date in your data is Month 8 (except the last 2 rows and then the Year is 2020 not the current Year.


    Therefore the date criteria is never met, ergo no result.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Hi KjBox , a trap for novices, I didn't actually need 1-10. I removed them and it's perfect.....thanks so much yet again.


    In regard to my other question,

    'Also, extra code to EXCLUDE current and previous month but includes ALL months prior ie if macro is run in Sept it only includes July and all prior months, needs to be dynamic.'


    Do I just need to change -1 to -2 after the Month(Date) in the code below?

    Code
    1. If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 7) = Category And x(i, 8) = InspType Then
  • That would work as long as the current Month is not January or February, that would make Month(Date) - 2 to return -1 and 0 respectively, when what you need is 11 and 12.


    To be fully dynamic you need to change

    Code
    1. If Year(x(i, DtCol)) = Year(Date) And Month(x(i, DtCol)) = Month(Date) - 1 And x(i, 3) = ContAreas(ii) And x(i, 7) = Category And x(i, 8) = InspType Then

    to

    Code
    1. If DateDiff("m", x(i, DtCol), Date) > 1 And x(i, 3) = ContAreas(ii) And x(i, 7) = Category And x(i, 8) = InspType Then

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.