473,395 Members | 1,730 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Excel Pasting Time / Manipulation - Help!

I am having a problems in with relation to time formats and would greatly appreciate any help.

The excel file I am writing the code for uses a Userform to prompt the user to select a file with various data - .txt format. I have to use the delimited options to preserve all the required data in separate cells (i.e. I can't paste into clipboard and then bring it into Excel as text). The problem is that the source program for the .txt file (which I have no control over) puts time into this format: 456:42:00 (example).

This represents 04:56:42 in UTC / 24hr time. The time is correct as displayed, but I need to reference the time in other subs to compare the times between multiple lines. When I try to use any time or time/date based formula ("Hours()", "Minutes()", "Seconds()", "DateDiff()", etc...) it reads the spot with "456" (sometimes 4 digits depending on the time) as hours (although it is actually both hours and minutes), the spot with "42" as minutes (which is actually seconds) and finally the spot with "00" as seconds, which is actually 'spoofer' info. Again, throw all the sarcastic comments you want at the format the time is spit out in, but that is the file format I need to work with.

I've tried the following:
Setting the .numberformat to "hh:mm:ss" or "[h]:mm:ss"
Setting .value = "=left()", as well as the Mid, and Right formulas
All of these just corrupts the time, since it doesn't see the hours, minutes and seconds in the right spot.

I also tried setting .text instead of .value with the left/right and mid, but get and "Object Required" error.

Any help? I tried to make it a textual explanation since the coding gets complicated.
Feb 26 '09 #1
2 2653
Stewart Ross
2,545 Expert Mod 2GB
It is most unfortunate that the exported data combines hours and minutes. In doing so the internal time value which Excel uses to represent the time will be completely incorrect - as you pointed out using your example of 456:00 which Excel simply cannot interpret in any meaningful way. Worse, because it is being stored as a date/time value after it is read into Excel you cannot manipulate it with string functions the way you have tried to do - nor can you convert it to a string easily because of the way Excel has tried to interpret the values it is seeing as hours but which are way out of range.

If you cannot get the output time format changed to standard hh:mm (or even into a straightforward single numeric value) I reckon you may have to pre-process your delimited text file in one of the following ways:

1. try prefixing the time field with a single apostrophe to force Excel to treat the value as text - at least then you could use string functions to extract the hours-minutes combination for subsequent separation, or

2. use a preprocessing routine to replace the non-standard time value with one that makes sense before it gets to Excel, or

3. design a custom VBA routine to read the whole of the delimited text file into the Excel sheet, converting the non-standard time value as it is read in. This is not as onerous as it sounds - but it is still a fair bit of work to loop through all lines of the file and split each line into its individual components.

Otherwise you will have to come up with a way to convert the meaningless date/time values to meaningful ones - but so far I've not come up with a viable way to rescue the dud values once they are in the sheet in date/time form.

-Stewart
Feb 27 '09 #2
kadghar
1,295 Expert 1GB
Oh, you'll find really interesting the string manipulation, and will help you to have exaclty what you want.

First of all, you do have control of your txt file, just google the syntax for OPEN / FOR / AS. This will help you import the text file into a string, or into a string array.

Since you're working in excel, you can use excel's dialogs to search the txt file. That'll really help you a lot. Google the APPLICATION.FILEDIALOG syntax

Once you have your string, you can use INSTR, MID, REPLACE, etc. to extract the numbers you want, and then asign them to a DATE variable.

e.g.
Expand|Select|Wrap|Line Numbers
  1. public sub ChangeTime(byref Str1 as string)
  2.     str1 = Replace(str1, ":", "")
  3.     str1 = Right(str1, Len(str1) - 2)
  4.     str1 = Mid(str1, 1, Len(str1) - 4) & ":" & _
  5.            Mid(str1, Len(str1) - 3, 2) & ":" & _
  6.            Right(str1, 2)
  7. end sub
HTH
Mar 6 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Marc | last post by:
Hi all, I am trying to write an application where I need the ability to open an Excel spreadsheet and do basic read/write, insert rows, and hide/unhide rows. Using win32com I have been able to...
2
by: Mrs Howl | last post by:
I don't know if there's even a way to do what I want. I click on a button in an Access form, and it opens an instance of Excel, and opens a workbook and runs a macro that's in it. So far, fine,...
6
by: Matthew Wieder | last post by:
I have the following requirements: Build a stand-alone C# application that asks the user to click in a cell in an Excel spreadsheet, and then displays the address of that cell in the C#...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
3
by: | last post by:
I wrote a class in VB.NET to export the contents of a datagrid to Excel. It works perfectly on my machine, but it fails on my customers' PCs that have identical versions of Win XP (SP1) and Excel...
1
by: Tom from Oakwood | last post by:
I'm writing VBA code in Excel 2000. I execute code which opens, one at a time, 10 Excel workbooks. It opens one, which does some manipulation of its own via a macro that executes upon opening,...
3
by: Bharathi | last post by:
Hi, I got strucked with reading date value from excel file using C#.NET. For Jan-2000 the value I am getting is 36526.0. For all other dates also I am getting some double value like this. ...
2
by: Krishna | last post by:
I was trying to delete rows in an existing .xls file using python. How do I do that? I was using the following code, it seem to work if I type in python window, but if I save it in text editor and...
24
rauty
by: rauty | last post by:
Hi all, I'm trying to access the most recent instance of Excel so I can paste data. What works for me is if Excel isn't already opened, I open it and can paste the data where I want to. What...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.