+ Reply to Thread
Results 1 to 14 of 14

How can time format be retained?

  1. #1
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    How can time format be retained?

    I am struggling to cope with time (aren't we all).
    I pick up a range of cells formatted on the worksheet as hh:mm. Put these into a macro and do some sorting. Then I place them on another worksheet but they arrive as time serials. The time serial appears as soon as the cells are placed in the macro.

    How can I retain the hh:mm format from one worksheet to the next without having to manually reformat the cells?

    Is there a VBA format method for individual array elements.

    Any help gratefully received.
    Last edited by macyarab; 02-24-2009 at 07:47 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: How can time format be retained?

    Without seeing your code it's hard to comment but in short:

    Please Login or Register  to view this content.
    (or paste special with formats... above may be simpler... unknown)

  3. #3
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: How can time format be retained?

    Looks to be just what I need I will give it a try and get back.

    Much appreciated for the quick reply

  4. #4
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: How can time format be retained?

    No luck,

    tried as follows :

    Please Login or Register  to view this content.
    Sub FillPoint(Temp, point, POINTXX, SURVLOC)

    Dim c As Long
    Dim R As Long
    Dim RA As Range
    Dim Rng As Range
    Dim i As Long

    Set Rng = Sheets(point).Range(Temp)

    For Each RA In Rng.Areas





    R = R + RA.Rows.Count
    Next RA




    ReDim POINTXX(R, 4)

    For Each RA In Rng.Areas
    For R = 1 To RA.Rows.Count


    i = i + 1
    For c = 1 To RA.Columns.Count

    POINTXX(i, c) = RA.Cells(R, c)

    'need to write code to force time format of column 1

    'Range("POINTXX(i, 1)").NumberFormat = "[hh]:mm"

    Format(RA.Cells(R, 1)).Value = "hh:mm"

    ' Range("Cells(R, 1)").NumberFormat = "[hh]:mm"

    'need to write code to force time format of column 1

    POINTXX(i, 4) = SURVLOC




    Next c
    Next R
    Next RA

    ''''''''''''''''''''' REMOVES BLANK ROWS (END OF PAGE ONLY)
    CUTENDPAGEBLANK POINTXX
    ''''''''''''''''''''' REMOVES BLANK ROWS (END OF PAGE ONLY)


    End Sub
    Please Login or Register  to view this content.
    [/CODE]
    Last edited by macyarab; 02-20-2009 at 01:30 PM.

  5. #5
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: How can time format be retained?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  6. #6
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Red face Re: How can time format be retained?

    oops my mistake

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: How can time format be retained?

    macyarab,

    I'm struggling to fathom what you're code is meant to be doing...

    I can determine that you are invoking this from elsewhere passing a worksheet, a range and 2 arrays (all defined as variants) ... you're then creating a range based off the sheet & range iterating through that ranges various areas to determine how many rows are contained within... redefining the first of the variant arrays... then populating the first variant array (POINTX) by iterating through each range area (row then column) however from there on I'm lost... are you then trying to write back the contents of the array somewhere else ?

    So in short... what is it you're trying to do...?

    I suspect you won't be surprised to hear that I think you may be able to achieve the same thing with far less aggravation... (& code)

  8. #8
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: How can time format be retained?

    You are quite right my code shows just how inept at this I really am. Hopefully I will be able to refine the code as I improve.

    what I am trying to do is track traffic flow across a number of data collection points.

    Somewhat inelegantly I have got it all working (too much code to place it all here) except that when I pass time from the original spreadsheets through my code via arrays and place it on the new sheet I lose the hh:mm format and end up with time serials .

    I can reformat on the final sheet but it takes quite some time to select the cells because they are spread throughout the final sheet.

    If I can find out how to format in VBA I could include the code somewhere in one of the numerous sub routines. Youre original suggestion appeared to be along the right lines but my lack of knowledge in adapting the code produced a syntax error, something about incorrect or missing object.

    Thank you for taking both the time (no pun intended) and effort to look at this.

    Part of the code that sorts the routes and places the masterpoints list on the spreadsheet sheet is shown below Column 1 of the masterpointslist contains the time that loses format during its travel.

    Please Login or Register  to view this content.

  9. #9
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: How can time format be retained?

    Sorry to be a pain but I'm utterly lost - I don't know what the new code is meant to represent - the first routine is incomplete and the second appears to refer to a Global variable masterpointlist which is not detailed anywhere in your code... also I can't fathom how this ties out to the code you provided in your penultimate post.

    I understand this is frustrating as it may seem obvious to you but it is equally frustrating on our part as we can't see the data or all of your code in context... are you able to provide a sample of your file ?
    (dummy the data if confidential obviously)

  10. #10
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: How can time format be retained?

    This may more simply explain my problem. both methods of populating the sheet result in non formatted time. (I do need to clear the range before writing the array to the sheet.)


    Please Login or Register  to view this content.
    Attached Files Attached Files

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: How can time format be retained?

    Which could be coded as per the below - with additional format line:

    Please Login or Register  to view this content.
    I guess my point is I very much doubt you need to use an array to store the values you want to copy... even if your range was non contiguous you can easily cycle the areas within and paste to next blank row etc... you're putting together a complex sub routine here and I think you're making life more difficult for yourself than need be ... not a bad thing in terms of a learning curve but frustrating at the same time I'm sure ;-) ... in the last code you posted you populate the array PointXX but it never gets used... you adopt a similar approach to the above with your use of point1A

  12. #12
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: How can time format be retained?

    Hi, the last code and sheet were purely set up as an example to try and explain the problem. The actual code is as you say more than likely overcomplex (a sign of my lack of knowledge unfortunately).

    I will try and incorporate the format method into the main code and get back. Is it possible to format the array element so that it retains a time hh:mm format?

    Thanks for your patience and help

  13. #13
    Registered User
    Join Date
    01-25-2009
    Location
    hemsworth, england
    MS-Off Ver
    Excel 2003
    Posts
    25

    Re: How can time format be retained?

    DonkeyOte, another windmill conquered. Thanks for talking me through the format command. Applied your code and all time was restored

    I didn't help by not knowing that I should have used .clearcontents instead of .clear.

    Just one last thing, is it possible to carry the format within the array or does the format have to be applied when placing the array onto the worksheet?

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: How can time format be retained?

    Time is a decimal value... noon = 0.5 ... 6am = 0.25 etc... the cell format is just that - it formats the decimal value to appear a certain way... so when referencing time you are storing decimal values... if you want to store as "12:00" you will need to store as actual text strings which would be a bit odd.

    I'm not going to get into it again but.... I remain very sceptical that you need to use an Array in your code in the first instance...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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