Open Side Menu Go to the Top
Register
Excel Strings Excel Strings

03-20-2012 , 04:58 PM
You have a string with potential values like:
1y25w5d7h7m7s

Or 1y7m

And I'd like to convert it all to seconds. Using a bunch of left/right/mid functions fails given the lack of ability to generalize. What do I need to read/learn in order to write

I know the logic of it (25w disambiguates months from minutes, sometimes there are two digits of data sometimes one) but just need to learn how to write code in excel.
Excel Strings Quote
03-20-2012 , 05:06 PM
Quote:
Originally Posted by DavidC
You have a string with potential values like:
1y25w5d7h7m7s

Or 1y7m

And I'd like to convert it all to seconds. Using a bunch of left/right/mid functions fails given the lack of ability to generalize. What do I need to read/learn in order to write

I know the logic of it (25w disambiguates months from minutes, sometimes there are two digits of data sometimes one) but just need to learn how to write code in excel.
Hi David,

Do you mean Excel as in a formula or in VBA?
Excel Strings Quote
03-20-2012 , 05:35 PM
Whatever's most appropriate, I'm therefore guessing vba
Excel Strings Quote
03-20-2012 , 06:00 PM
Quote:
Originally Posted by DavidC
Whatever's most appropriate, I'm therefore guessing vba
Scan the string from left to right (say, using a for loop, like for i = 1 to len(s), then using mid(s,i,1) to examine the current character), while storing numeric characters in a temporary string buffer. Whenever you encounter a non-numeric character, y/w/d/h/m/s, you take whatever is in the buffer, and convert it to a number (CInt/CDbl), then to seconds, add it to a running total, and empty the buffer.

Depending on your error-checking needs, you may want to detect whether a given unit's been used twice, whether the units are in the right order, whether the input string ends with a unit as opposed to a number, etc.
Excel Strings Quote
03-22-2012 , 09:32 PM
lol it took me a while to understand that, I get it now, ty good idea.

What I meant though is more like what book should I read and how do you insert code into an excel sheet?
Excel Strings Quote
03-23-2012 , 12:02 PM
Quote:
Originally Posted by DavidC
lol it took me a while to understand that, I get it now, ty good idea.

What I meant though is more like what book should I read and how do you insert code into an excel sheet?
You can access the VBA editor from the developer tab in Excel. Once there, create a new module and write a new sub.

Here's a good article to get you started: http://www.wiseowl.co.uk/blog/s140/vba_primer_pt2.htm

One of my preferred ways to run macros from an excel spreadsheet is to place a button on the spreadsheet that activates the macro when clicked. Hope this answers your question.
Excel Strings Quote
03-23-2012 , 06:58 PM
Thanks man.
Excel Strings Quote

      
m