+ Reply to Thread
Results 1 to 7 of 7

Forcing numbers to be 2 digits when adding to the middle of a string.

  1. #1
    Registered User
    Join Date
    06-17-2019
    Location
    USA
    MS-Off Ver
    Mac 16.75.2 (365)
    Posts
    65

    Forcing numbers to be 2 digits when adding to the middle of a string.

    Hi all: I posted this as a continuation on a different thread, but it was recommended that I repost it as a new thread:

    I work in a transportation field and am responsible for rail schedules. I am trying to add a fixed number of minutes to the row above. The problem is that the display formats of time that are available (from the format menu) don't match the industry standard (6:21A, 10:14P). Also, we sometimes add text in front of the time which have different meanings to our crews (S 6:21A, L 10:14P). Due to these anomalies, I have discovered that it is easier to keep things in "general" or "text" format.

    Unfortunately this leads to a few complications when adding a fixed number of minutes to the cell above. So far, I have the following formula. For Cell D122, I use the following: =IF(LEN(D121)=5,LEFT(D121,2)&(MID(D121,3,2)+3)&(RIGHT(D121,1)),LEFT(D121,3)&(MID(D121,4,2)+3)&(RIGHT(D121,1))). The reason for the if statement is whether the hours is a one digit number or a 2 digit number.

    this formula works great, except for when the minutes (positions 3/4 or 4/5) are between 00 and 07, because when I add the 2 minutes to these numbers, I end up with a 1-digit number between 2 and 9.

    For example, adding 2 to the 3rd and 4th characters of 6:01A turns unto 6:3A, when're I want 6:03A.

    Is there any way to force the results to be a 2 digit number?

    One solution is to add a hidden row in-between with the results of the addition, force it into a 2-digit number using the formatter, and then joining the pieces together, but I was hoping to just add a string into the middle of my formula.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,869

    Re: Forcing numbers to be 2 digits when adding to the middle of a string.

    We have no idea what the source data looks like, so the formulae you have tried are meaningless.

    =TEXT(A1,"00")

    would force the value in A1 to appear as two digits, so 9 would appear as 09.

    Hope this helps.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    06-17-2019
    Location
    USA
    MS-Off Ver
    Mac 16.75.2 (365)
    Posts
    65

    Re: Forcing numbers to be 2 digits when adding to the middle of a string.

    The source Data would be say "S 9:05A". When I use the formula to try and add 3 minutes to it, I get "S 9:8A". My goal is is to force the "8" to be an "08". I don't want to manually add a "0" into it, because I don't want "S 9:09" to convert to "S 9:011A"

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,869

    Re: Forcing numbers to be 2 digits when adding to the middle of a string.

    I wasn't suggesting you add 0 manually. Try this:

    =IF(LEN(D121)=5,LEFT(D121,2)&(MID(D121,3,2)+3)&(RIGHT(D121,1)),LEFT(D121,3)&(TEXT(MID(D121,4,2)+3),"00")&(RIGHT(D121,1)))

    If this doesn't work, provide a sample workbook.

  5. #5
    Registered User
    Join Date
    06-17-2019
    Location
    USA
    MS-Off Ver
    Mac 16.75.2 (365)
    Posts
    65

    Re: Forcing numbers to be 2 digits when adding to the middle of a string.

    Ok, I can't post any links yet, since I haven't made enough posts. Give me a day or 2 and I'll try and respond to some other threads.

  6. #6
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Forcing numbers to be 2 digits when adding to the middle of a string.

    You can add a sample file to your first post as soon as you join.

    Click the 'Go Advanced' button in the bottom right corner, then scroll down and look for the 'Manage Attachments' link (text link, not paperclip icon) and follow the prompts.

  7. #7
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Forcing numbers to be 2 digits when adding to the middle of a string.

    Maybe...

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

+ 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] Adding numbers based on the value of a number in the middle of the cell
    By Chris McGlothen in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-07-2018, 05:03 PM
  2. Replies: 1
    Last Post: 02-19-2016, 12:46 PM
  3. [SOLVED] Adding in Information in the middle and end of the string
    By RaydenUK in forum Excel General
    Replies: 4
    Last Post: 05-13-2014, 12:28 PM
  4. [SOLVED] Adding variable to middle of string for querytables.add (to import daily files)
    By JP Romano in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-12-2012, 01:25 PM
  5. Adding digits onto a string of numbers
    By robbiebrown34 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-17-2008, 02:19 AM
  6. [SOLVED] adding a leading 0 to only the numbers with 4 digits
    By SwampYankee in forum Excel General
    Replies: 4
    Last Post: 06-01-2006, 03:55 PM
  7. adding digits to front/end of fax numbers
    By Luke in forum Excel General
    Replies: 2
    Last Post: 04-27-2005, 07:06 PM

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