+ Reply to Thread
Results 1 to 8 of 8

overflow error when using DateAdd

Hybrid View

  1. #1
    Registered User
    Join Date
    06-13-2015
    Location
    NC, USA
    MS-Off Ver
    Office 2016 for Mac
    Posts
    29

    overflow error when using DateAdd

    Here is my code:


    Option Explicit
    
    Dim month as String
    Dim currentDateAndTime as Date
    Dim i as Integer
    
    currentDateAndTime = Now
    
    For i = -11 to -1
          month = Format(DateAdd("m", CDbl(i), currentDateAndTime), "mmmm")
    Next i

    About half the time, I get a runtime error '6' Overflow error.

    What gives????

    Thanks for the help!

  2. #2
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: overflow error when using DateAdd

    Do you need to do this : CDbl(i) ?
    Why not
    month = Format(DateAdd("m", i, currentDateAndTime), "mmmm")
    ?
    .. and don't forget to have fun!
    Bogdan.

    mark SOLVED and Add Reputation if my answer pleases you

  3. #3
    Registered User
    Join Date
    06-13-2015
    Location
    NC, USA
    MS-Off Ver
    Office 2016 for Mac
    Posts
    29
    Quote Originally Posted by bulina2k View Post
    Do you need to do this : CDbl(i) ?
    Why not
    month = Format(DateAdd("m", i, currentDateAndTime), "mmmm")
    ?
    Yes that was actually my original code. I added the CDbl as an attempt to fix the issue. (Which of course it did not lol)

  4. #4
    Registered User
    Join Date
    06-13-2015
    Location
    NC, USA
    MS-Off Ver
    Office 2016 for Mac
    Posts
    29

    Re: overflow error when using DateAdd

    Also, it should be noted that I am running Excel for Mac 2016

  5. #5
    Valued Forum Contributor bulina2k's Avatar
    Join Date
    11-20-2012
    Location
    Urziceni, Ialomita, Romania
    MS-Off Ver
    2019 and 365
    Posts
    867

    Re: overflow error when using DateAdd

    I could not recreate the error but I would try to declare i as Long. Have you tried that?

  6. #6
    Registered User
    Join Date
    06-13-2015
    Location
    NC, USA
    MS-Off Ver
    Office 2016 for Mac
    Posts
    29

    Re: overflow error when using DateAdd

    OK, I just tried declaring i as Long, and it did not work. I then removed the CDbl(), which also did not change anything.

    Here is the code in it's entirety for this function:


    Public invoiceList As String
    Public VRFolderPath As String 'There is another function which runs first and sets VRFolderPath
    Public currentDateAndTime As Date '(There is another function which runs first and sets currentDateAndTime
    
    
    Sub getUnpaidInvoicesForPreviousMonths(ByVal currentMonth As String, ByVal Client As String)
        Dim currentWorkBook As Workbook
        Dim folderToLookup As String
        Dim mySplit As Variant
        Dim i As Long
        Dim itemString As String
        Dim lastSep As String, extensionSepLocation As Integer
        Dim month As String
        
        invoiceList = ""
        
        'Lookup Unpaid Invoices for the last 12 months
        For i = -11 To -1
            If Day(currentDateAndTime) < 15 Then
                month = Format(DateAdd("m", i - 1, currentDateAndTime), "mmmm")
            Else
                month = Format(DateAdd("m", i, currentDateAndTime), "mmmm")
            End If
            folderToLookup = Replace(VRFolderPath, "/OneDrive", "/OneDrive/" & month & "/Unpaid")
            folderToLookup = MacScript("tell text 1 thru -2 of " & Chr(34) & folderToLookup & Chr(34) & " to return quoted form of it's POSIX path")
            addInvoicesInFolderToArray folderToLookup, Client
        Next i
        
        'Populate unpaid invoices for last 11 months
        If invoiceList <> "" Then
            With Application
                .ScreenUpdating = False
            End With
            
            Sheets("Client Statement").Select
            Range("C27:I34").ClearContents
            
            mySplit = Split(invoiceList, Chr(13))
            For i = LBound(mySplit) To UBound(mySplit)
                On Error Resume Next
                itemString = mySplit(i)
                lastSep = InStrRev(itemString, Application.PathSeparator, , 1)
                extensionSepLocation = InStrRev(itemString, ".", , 1)
                
                If i <= 7 Then
                    Sheets("Client Statement").Cells(i + 27, 3).Value = Left(Mid(itemString, lastSep + 1, Len(itemString)), 5)
                    Sheets("Client Statement").Cells(i + 27, 4).Value = DateSerial(Year(Now), Left(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2), Right(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2))
                    Sheets("Client Statement").Cells(i + 27, 5).Value = Right(Left(itemString, InStrRev(itemString, ".") - 1), Len(Left(itemString, InStrRev(itemString, ".") - 1)) - InStrRev(itemString, " "))
                Else
                    Sheets("Client Statement").Cells((i - 8) + 27, 7).Value = Left(Mid(itemString, lastSep + 1, Len(itemString)), 5)
                    Sheets("Client Statement").Cells((i - 8) + 27, 8).Value = DateSerial(Year(Now), Left(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2), Right(Right(Left(Mid(itemString, lastSep + 1, Len(itemString)), 10), 4), 2))
                    Sheets("Client Statement").Cells((i - 8) + 27, 9).Value = Right(Left(itemString, InStrRev(itemString, ".") - 1), Len(Left(itemString, InStrRev(itemString, ".") - 1)) - InStrRev(itemString, " "))
                End If
                
                Set currentWorkBook = Application.Workbooks.Open(itemString)
                
                With currentWorkBook
                    .Activate
                    .ExportAsFixedFormat Type:=xlTypePDF, Filename:=Left(itemString, extensionSepLocation) & ".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False
                    .Close savechanges:=False
                End With
                
                On Error GoTo 0
            Next i
            
            With Application
                .ScreenUpdating = True
            End With
        Else
            MsgBox "There are no unpaid invoices for this client for the previous month of " & currentMonth & "."
            
            With Application
                .ScreenUpdating = True
            End With
        End If
    
    End Sub

  7. #7
    Registered User
    Join Date
    06-13-2015
    Location
    NC, USA
    MS-Off Ver
    Office 2016 for Mac
    Posts
    29

    Re: overflow error when using DateAdd

    I am still having the error.. Does anyone have an idea?? Please and thank you!!

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: overflow error when using DateAdd

    What are the values of all the variables in the statement when the code errors?
    Entia non sunt multiplicanda sine necessitate

+ 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. [SOLVED] dateAdd Overflow
    By rmweaver81 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-03-2013, 01:15 PM
  2. Overflow error
    By VelvetRevolver84 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-23-2013, 11:50 PM
  3. Explanation of the Run-time error '6': Overflow Error
    By mgphill in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2012, 10:46 AM
  4. Want to do a while-loop but get error message: run error '6' overflow
    By danzi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-07-2011, 01:48 PM
  5. Msg error (overflow)
    By Mamud in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 09-25-2011, 10:54 AM
  6. Overflow error
    By share knowledge in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-02-2010, 02:07 AM
  7. Error 6 Overflow
    By tekapo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-03-2010, 05:07 AM
  8. Overflow Error
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-10-2010, 02:08 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