+ Reply to Thread
Results 1 to 6 of 6

I'm stuck on Formatting a Date

Hybrid View

  1. #1
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    81

    I'm stuck on Formatting a Date

    I've been formatting a few dates throughout hundreds of lines of code and it works. Now I'm down to the last few lines of my program, and I'm getting an overflow on a date I'm trying to reformat. To simplify things, A wrote a few lines of code that show what I'm trying to do. What am I doing wrong?

    Image1.jpg
    Last edited by JeffLeites; 02-27-2020 at 01:48 PM.

  2. #2
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: I'm stuck on Formatting a Date

    DateYYYYMMDD is a string and you want to format it as date ...!
    Change it to a date
    - Battle without fear gives no glory - Just try

  3. #3
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    81

    Re: I'm stuck on Formatting a Date

    That didn't help

    Image3.jpg

  4. #4
    Forum Expert
    Join Date
    12-24-2007
    Location
    Alsace - France
    MS-Off Ver
    MS 365 Office Suite
    Posts
    5,066

    Re: I'm stuck on Formatting a Date

    See next code

    Option Explicit
    
    Sub MySub()
    Dim DateYYYYMMMDD As Date
    Dim DateMMDDYYYY  As String
    
    
    DateYYYYMMMDD = DateSerial(2020, 3, 1)
    
    DateMMDDYYYY = Format(DateYYYYMMMDD, "mmddyyyy")
    MsgBox ("MMDDYYYY = " & DateMMDDYYYY)
    
    End Sub

  5. #5
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    81

    Re: I'm stuck on Formatting a Date

    That works as a test, but in my real application I have these lines that do work:

    Dim CSVDate As String
    Dim CSVDateYYYMMDD As String

    CSVDateYYYMMDD = Format(CSVDate, "yyyymmdd")

    I don't see the difference between that and what I'm trying to do now that gives me an overflow:

    Dim PostingLogDate As String
    Dim PostingLogDateMMDDYYYY As String As String

    PostingLogDateMMDDYYYY = Format(PostingLogDate, "mmddyyyy")
    Last edited by JeffLeites; 02-27-2020 at 03:43 PM. Reason: Fix mistakes in code lines

  6. #6
    Registered User
    Join Date
    12-25-2012
    Location
    Los Angeles
    MS-Off Ver
    Excel 2013
    Posts
    81

    Re: I'm stuck on Formatting a Date

    I gave up trying to figure it out and did it the long way:

    Sub Button1_Click()
    Dim DateYYYYMMDD As String
    Dim DateMMDDYYYY As String
    Dim DateYYYY As String
    Dim DateMM As String
    Dim DateDD As String

    DateYYYYMMDD = "20200227"
    DateYYYY = Left(DateYYYYMMDD, 4)
    DateMM = Mid(DateYYYYMMDD, 5, 2)
    DateDD = Right(DateYYYYMMDD, 2)
    DateMMDDYYYY = DateMM & DateDD & DateYYYY

    MsgBox ("MMDDYYYY = " & DateMMDDYYYY)

    End Sub

    I don't know why format would work going from mmddyyyy but not yyyymmdd

+ 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. Stuck on Date formula
    By GSevensM in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 09-14-2019, 01:37 PM
  2. [SOLVED] Conditional formatting Stuck in Range
    By pipsmultan in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 11-04-2015, 09:29 AM
  3. Replies: 5
    Last Post: 11-05-2014, 03:34 PM
  4. Conditional formatting stuck
    By M1234 in forum Excel General
    Replies: 12
    Last Post: 10-01-2013, 09:05 AM
  5. [SOLVED] Date format stuck
    By Noxia in forum Excel - New Users/Basics
    Replies: 7
    Last Post: 07-15-2013, 04:47 AM
  6. Stuck at work and stuck on a count function
    By gregfetzer in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-24-2012, 12:47 AM
  7. [SOLVED] Conditional Formatting versus macro code inthis example -stuck how to solve myself
    By leanne2011 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-14-2012, 08:11 PM

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