0

I have a vba automation to concatenate some text but it's been giving me the "Overflow" error when changing the result to date in this specific file (works fine in others).

Function FormatAsDate(value As Variant, isDateColumn As Boolean) As String
    ' Format the value as a date if it's a valid date or a date serial number
    If isDateColumn And IsNumeric(value) Then
       ** FormatAsDate = Format(CDbl(value), "mm/dd/yyyy")**
    Else
        FormatAsDate = CStr(value)
    End If
End Function

I've tried to use less rows (down to 10 rows) and nothing. Could it be hidden formatting in my file? Please help!

8
  • 1
    When it fails what's the value of value ? Commented Aug 21, 2024 at 21:48
  • You should always be suspicious of hidden characters in untrusted data (think - user input, unknown input). For instance, I can't tell you how many times leading or trailing spaces cause errors, or unprintable characters copied in with copy/paste from web pages. Commented Aug 21, 2024 at 21:59
  • @TimWilliams: : value : 10052026 : Variant/Double Commented Aug 21, 2024 at 22:07
  • 1
    That value is too large to convert to a date. ? format(10052026,"mm/dd/yyyy") in the Immediate window also gives "overflow" Commented Aug 21, 2024 at 22:09
  • 1
    Without the rest of the code and some sample data there's not much else we could suggest. Commented Aug 21, 2024 at 22:20

1 Answer 1

0

Try something like this:

If value Like "##/##/####" then Return(CDate(value))
If CLng(value) > 40000 and CLng(value) < 50000 then Return(CDate(value)) ' 2009 - 2036
If value Like "########" Then Return(DateSerial(Right(value,4), Left(value,2), Mid(Value,3,2))
Debug.Print "Error..."
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.