I am having strange results from DIM in vba for excel on MAC

Bryon Karren 1 Reputation point
2020-12-25T00:08:50.33+00:00

I am wondering if this same issue arises on a Windows platform.

If I Dim dates the last variable of the dates always wants to assume a Time format and appears as String*-1. All the others become Variant/String*-1. So this last date variable does not function as a Date variable in the normal fashion. I haven't tested but it seems to be a Time. I'm not sure my screen shot is uploading properly but its very straight forward. This is very strange behaviour and quite confusing. Always must declare a dummy date to get around it.

Dim Date1, Date2 as date

then in watch window
Watch : : Date1 : 2020-12-24 : Variant/String * -1 : Module7.ShowDateTimeIssue
Watch : : Date2 : 00:00:00 : String * -1 : Module7.ShowDateTimeIssue

51054-screen-shot-2020-12-24-at-154952.png

Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. HansV 966 Reputation points MVP
    2020-12-25T11:13:57.353+00:00

    I don't know why the Watch window shows Date1 and Date2 as strings. Perhaps Mac stores dates as strings?

    But I can explain the difference: in a Dim statement, you have to specify the data type of each individual variable, otherwise they default to Variant. So the line

    Dim Date1, Date2 As Date

    is equivalent to

    Dim Date1 As Variant, Date2 As Date

    If you want to declare both as dates, use

    Dim Date1 As Date, Date2 As Date

    1 person found this answer helpful.

  2. HansV 966 Reputation points MVP
    2020-12-25T23:19:53.047+00:00

    See for example viewtopic.php

    0 comments No comments

  3. Bryon Karren 1 Reputation point
    2020-12-25T23:48:18.79+00:00

    Ah now I see it in the documentation. An earlier reply to you put me onto it. I was looking at the same article not realizing it was VB, not VBA.

    type Optional. Data type of the variable; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String (for variable-length strings), String length (for fixed-length strings), Object, Variant, a user-defined type, or an object type. Use a separate As type clause for each variable you declare.

    https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/dim-statement

    Thanks, it’s always nice to get the right answers.

    0 comments No comments

  4. Bryon Karren 1 Reputation point
    2020-12-30T05:48:58.49+00:00

    OK now I've got that sorted out it seems that MAC vba is very limited when it comes to data types. Thanks to my not realizing that every variable in a DIM statement must have a matching "as"; most of the variables I was using in a rather large project for the past 10 years ended up as variants. The project runs on a Windows machine but I have been using a MAC for about 5 years and was able to run it on both. Recently when making some changes to the code however I started to see some issues: (Error 6)

    • there really are no integer, single or double variables even when properly declared. If you put them in WATCH you see they are only strings. Oddly ones you call an integer can be assigned a number, even multiplied, but not divided. Single and Double can not even be assigned a number (xDbl=1) or you get the dreaded ERROR 6
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.