+ Reply to Thread
Results 1 to 16 of 16

Date to text conversion for a column

  1. #1
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Date to text conversion for a column

    Hi All, first request for help and it feels great to have somewhere else to turn apart from banging my head against a brick wall.

    I have a workbook, with various worksheets, each hold different data which is validated on the sheet and when the user has fixed any issues they press a button that saves the file as a csv.. simple.. but no.

    One of the worksheets has a column for date, which is displayed in the format "yyyy-mm-dd hh:mm:ss", but when looking at the actually contents of the cell in the edit bar it appears as "yyyy//mm/dd hh:mm:ss"

    I've got the file creation working nicely, but when I open the file it appears as a number, so when opening it in excel it displays as the yyyy/mm/dd hh:mm:ss. I need the csv to hold the date as yyyy-mm-dd hh:mm:ss as this data gets loaded into an enterprise platform and not an option to change it.

    I need the data change for a range of cells in E5:E5000

    I've unsuccessfully tried the TEXT(),I'm sure there's a way to do it as seem similar examples, but not one for this situation

    Any ideas gratefully received.
    Thanks

    PS- should this be in the Commercial section, if so what level of points, feels like a relatively simple one, but as I don't know the solution no idea)
    Last edited by enthusiastic amateur; 06-07-2017 at 03:22 PM. Reason: Just saw commercial section - added PS

  2. #2
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    Hi EA, welcome to Excel Help Forums! This should convert a serial date in B4 to properly formatted text:
    =TEXT(B4,"yyyy-mm-dd hh:mm:ss")

    You should be able to copy the cell with the formula and paste its VALUE in the csv.

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 06-07-2017 at 03:27 PM.

  3. #3
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Re: Date to text conversion for a column

    Wow, that was quick. Thank you so much
    How, do I select the columns it needs to apply to... I am very new at this and not sure how to create the sub for this successfully

  4. #4
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    Wait, sorry, you want to do this in vba. Back in a few...
    Ok, try this:
    Please Login or Register  to view this content.

    NOPE, that doesn't work...

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 06-07-2017 at 03:48 PM.

  5. #5
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    WOW, Excel REALLY hates Dates as text. Only way I could do it is to put an apostrophe in front:
    Please Login or Register  to view this content.
    I ran it on some test data, saved as .csv, then opened the csv in Notepad. Appears to save correctly (no apostophe).

    Please click the Add Reputation star below any helpful posts, and if you have your answer, mark your thread as SOLVED (Thread Tools up top). Thanks!-Lee
    Last edited by leelnich; 06-07-2017 at 05:42 PM.

  6. #6
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Re: Date to text conversion for a column

    THanks Lee,
    I've just added it in and tried to run it and got an error message.

    .errormessage.PNG

    This is the first action as part of the file save routine I have,
    as I want to convert the data first, then asks for the range to be saved and then it saves it as a csv

    The full code (cobbled together from looking all over the place is)...but the rest of it works but I do want to remove the define the range bit to get it to work it out itself, but thats a battle for another day

    PS You'll get a view on how out of my depth I really am I suspect by the code below, but learning a lot with each problem

    Sub Save_CSV_5_Offer_EnterRange()


    Range("E5:E5000").NumberFormat = "yyyy-mm-dd hh:mm:ss"

    For rw = 5 To 5000
    Range("E" & rw).Value = .Text
    Next





    Dim MyPath As String
    Dim MyFileName As String
    Dim WB1 As Workbook, WB2 As Workbook

    Set WB1 = ActiveWorkbook
    MyFileName = "Offer-" & Format(Date, "yyyymmdd-") & Format(Time, "hhmmss")
    FullPath = WB1.Path & "\" & MyFileName

    '(1) either used range in active sheet..
    'ActiveWorkbook.ActiveSheet.UsedRange.Copy

    '(2) or alternatively, user selected input range:
    Dim rng As Range
    Set rng = Application.InputBox("select cell range with changes", "Cells to be copied", Default:="A5:J", Type:=8)
    Application.ScreenUpdating = False
    rng.Copy

    Set WB2 = Application.Workbooks.Add(1)
    WB2.Sheets(1).Range("A1").PasteSpecial xlPasteValues

    Application.DisplayAlerts = False
    'save file
    If Not Right(MyFileName, 4) = ".csv" Then MyFileName = MyFileName & ".csv"
    With WB2
    .SaveAs Filename:=FullPath, FileFormat:=xlCSV, CreateBackup:=False
    .Close False
    End With
    Application.DisplayAlerts = False
    End Sub

  7. #7
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    I changed post# 5 code. Please give that a try. Thanks - Lee

    ps. Range is on ActiveSheet by default. If you want cells from a different sheet, you must qualify the reference.
    Last edited by leelnich; 06-07-2017 at 05:48 PM.

  8. #8
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Re: Date to text conversion for a column

    Yeah!!! Thanks. I confused myself to begin with by looking at the generated files in excel.. dooh.. when looking in Notepad...there were those lovely formatted columns... now I have de-construct the code so I can work learn from it... thank you

  9. #9
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Re: Date to text conversion for a column

    Hi Lee, This works and creates the output, would you be surprised that it takes a over 10 minutes to run for the 5000 rows?
    Thanks
    Graham
    Last edited by enthusiastic amateur; 06-07-2017 at 06:43 PM.

  10. #10
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Date to text conversion for a column

    See if this works and is quicker:

    Please Login or Register  to view this content.

    On second thought - Nevermind
    Last edited by xladept; 06-07-2017 at 07:59 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  11. #11
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    Very surprised. Try the following in future. Same code, but I turned off ScreenUpdating and Calculation- the usual suspects for slow processing:
    Please Login or Register  to view this content.
    ps. How long was it taking before you added the conversion code?
    pps. BTW, your last line of code should be Application.DisplayAlerts = True.
    Last edited by leelnich; 06-07-2017 at 08:40 PM.

  12. #12
    Registered User
    Join Date
    06-07-2017
    Location
    Nottingham, UK
    MS-Off Ver
    2013
    Posts
    15

    Re: Date to text conversion for a column

    Hi Lee, That's done the trick, much quicker.

    Before the new version of code, with 5000 rows to check, I actually gave up waiting as it was over 10 mins and instead reduced the row quantity down to speed it up.

    The new version takes seconds to do the 5000.. and I also added another column into the sub (the end date) as a second For/Next group.. worked like a dream and I didn't notice any discernible speed difference...happy days!

    Thank you so much for your help with it Going to mark this as solved (once I work out how).

    I also changed the setting as per you PPS, I'd turned this off to stop any odd messages appearing to the users, but setting it to True hasn't made a difference.

    Now to look through the code to learn how it works for "next time"... the head scratching bit
    Last edited by enthusiastic amateur; 06-08-2017 at 05:17 AM. Reason: typos

  13. #13
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    Excellent! Glad I could help, thank you for the rep. - Lee

  14. #14
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Date to text conversion for a column

    I guess I'm confused - I assumed that you did this first and it didn't work - it works on my machine:

    Please Login or Register  to view this content.

  15. #15
    Forum Expert leelnich's Avatar
    Join Date
    03-20-2017
    Location
    Delaware, USA
    MS-Off Ver
    Office 2016
    Posts
    2,807

    Re: Date to text conversion for a column

    When you say "it works..." what do you mean? The challenge was not simply to display the format, but rather to convert the number to text, and return it to the sheet without Excel automatically coercing it back to a number. I originally thought I could find a method that would work on the entire range, but after several false starts conceded to reading the range into an array, looping thru that to change the NUMBERS to formatted text with apostophe prefixed, then reading the array back to the sheet. I could also have changed the CELL formatting to text (@) to skip the apostophe, but the loop would still be required to change the NUMBERS. That's the best I could do, but I'm always open to a better way!

  16. #16
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Date to text conversion for a column

    Hi leelnich,

    I feel foolish now that you re-explain. My bad - nevermind

+ 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] Date Conversion-Text to DD/MM/YYYY
    By Prabudas in forum Excel General
    Replies: 2
    Last Post: 01-22-2016, 04:45 AM
  2. [SOLVED] Text Date conversion
    By wipidu in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-20-2014, 08:43 AM
  3. [SOLVED] Preventing Date Conversion from Text
    By mirfield in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2012, 10:46 AM
  4. Year Date Conversion To Text
    By ComradeBT in forum Excel General
    Replies: 2
    Last Post: 10-08-2010, 08:03 AM
  5. conversion of text to time and date
    By afgi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-30-2010, 12:08 PM
  6. Date to Text Conversion
    By gvb in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-19-2008, 09:33 AM
  7. automatic conversion of text into date :(
    By arnold_charming in forum Excel General
    Replies: 2
    Last Post: 12-04-2005, 03:50 PM

Tags for this Thread

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