Consulting

Results 1 to 7 of 7

Thread: Cells Formatting for DATE

  1. #1

    Cells Formatting for DATE

    Hi,

    I need a help for formatting the DATE fields in a sheet which appears in General Format.

    The situation is as follows -
    • I am fetching data in columns from DB (Total 55 in DB out of which few or all are selected by the user). There are few date columns (like Arr_DT, Dep_DT etc)
    • After getting data in Sheet, the cell format for DATES is seen as general. For example - We can see date as 38500 instead of 5/28/05.
    I tried following code, but most of the columns are getting converted to DATE format.

    Cells.Find(What:="_DT", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _, SearchFormat:=False).Activate

    ActiveCell.Columns.EntireColumn.Select
    Selection.NumberFormat = "mm/dd/yy ;@"

    For i = 0 To ListBox1.ListCount
    ' This is the count of columns selected by USER. i.e This many times I need to iterate & see whether there are any columns having "_DT" text.


    Cells.FindNext(After:=ActiveCell).Activate
    ActiveCell.Columns.EntireColumn.Select
    Selection.NumberFormat = "mm/dd/yy ;@"
    Next


    What I need is ...if user selects 25 columns & there are 3 DATE columns, then using this code we should be able to identify these 3 DATE fields & then format only those columns.
    ......In short only DATE fields should be identified and formatted & OTHERS should be remaining in the same format as got from DB.

    Currently I am getting some wrong results...Can anyone guide me here or propose any other alternative to acheive this requirement?

    Thanks!


  2. #2
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    I ran your code and it seemed to work fine. What sort of "wrong results" are you getting? Can you post a workbook with some sample data that is similar to what you are working with?

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    If you're bring in the data from an external source, 38500 might be text = "38500".

    Inside a loop, try making each cell a number:

    .Value = --.Value

    and see if the date format takes

    Paul

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Paul,

    -- is an Excel construct, not VBA.

    .Value = .Value

    should sort it out.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by xld
    Paul,

    -- is an Excel construct, not VBA.

    .Value = .Value

    should sort it out.

    At least in 2007, either seems to actually work, but your's is shorter.

    The important thing is to get the values into numbers so that a Date format will work.

    [VBA]
    Sub drv()

    With Worksheets("sheet1").Cells(1, 1)
    .Clear
    .NumberFormat = "General"
    .Value = "'38500"
    MsgBox TypeName(.Value)

    ' .Value = .Value
    ' MsgBox TypeName(.Value)

    .Value = --.Value
    MsgBox TypeName(.Value)

    End With
    End Sub
    [/VBA]

    Paul

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Of course it does, it is truly double-negating LOL!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by xld
    Of course it does, it is truly double-negating LOL!
    I was surprised that setting a .Value containing a string to itself would do a type conversation and make it into a double

    I knew from previous posts that VBA would do the type conversion for a 'numeric string' to a number if you used the negation operator, and another to flip the sign of the 'now a number' again.

    Paul

Posting Permissions

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