+ Reply to Thread
Results 1 to 4 of 4

data formats in Excel VBA

  1. #1
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Pendleton, OR
    MS-Off Ver
    Excel 2013
    Posts
    129

    data formats in Excel VBA

    I always seem to have trouble with type-casting in VBA. I have a column of dates I want to access but I get errors (Type mismatch) no matter how I declare my variables. The data has a date and time column. Since I am not using time in this macro, I had declared the variable as Dim dateAcq As Long and dateAcq = Cells(i,j). The data in i,j is formatted as a date and should be a whole number.

    I get Type mismatch errors with Integer, Long, Double, String, …
    and even when I use an integer function dateAcq = Int(Cells(i,j)).

    The macro seems to run well if these functions are undeclared Variants, but I am curious why I am getting errors.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: data formats in Excel VBA

    It can't be an integer as it has only 32678 bits. Why can't you declare it as a date? Regardless of the value has time, it should be declared as a date. The error might be somewhere else. Could you attach the sample?

  3. #3
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Pendleton, OR
    MS-Off Ver
    Excel 2013
    Posts
    129

    Re: data formats in Excel VBA

    Thanks. I can't attach anything at the moment because I am running a very long macro. When I compare two dates I want to make sure it is only looking at the integer portion. Usually these are entered as dates or copied from other spreadsheets, and should be clean date formats without any fractional part, but I have no guarantee of that.

    Since a typical file will have thousands of lines - mostly machine generated - I am never quite sure. The dates are attached by users in the field.

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: data formats in Excel VBA

    If you want to omit the time bit and only want to compare the date, use CLng— which converts the decimal in to long

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 04-12-2015, 06:51 AM
  2. Replies: 1
    Last Post: 11-06-2014, 05:23 PM
  3. Replies: 4
    Last Post: 06-16-2014, 11:23 PM
  4. Replies: 0
    Last Post: 06-10-2013, 03:36 PM
  5. Converting excel data in Word with formats
    By rdaboy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-05-2011, 09:58 AM
  6. Converting Mainframe DISPLAY data to EXCEL formats
    By Peter in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-11-2006, 03:10 PM
  7. Excel could not save all of your data and formats
    By Alex J in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-07-2005, 12:06 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1