+ Reply to Thread
Results 1 to 10 of 10

VBA code to replace "1" with "7:00/ 15:30"

  1. #1
    Registered User
    Join Date
    11-05-2016
    Location
    Constanta
    MS-Off Ver
    2010
    Posts
    13

    VBA code to replace "1" with "7:00/ 15:30"

    Hi, everyone,

    I am an VBA newbie to say at most. Please help me! I have searched through dozens of post, but nothing worked for me. Or maybe i don't know what to search

    I have an excel workbook with more than 2 or 3 sheets. It's an schedule for the place i work at. We are working in shifts: 1st is from 7 to 15:30, 2nd is from 13:30 to 22:00 and 3rd is from 22:00 to 06:30.
    As mentioned in the title, i need some code (or any idea) that will automatically replace 1, 2 or 3 with the above hour intervals.
    I want it to happen in real time. I type 1, press Enter and poof!, 7:00/ 15:30 appears instead of 1. And so on for 2 and 3.


    Thank you all !
    Last edited by GLV; 03-24-2023 at 08:45 AM.

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,376

    Re: VBA code to replace "1" with "7:00/ 15:30"

    VBA is Macros. Seems you have a contradiction in your requirements.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Quote Originally Posted by GLV View Post
    But with no macros.
    I am a little confused here. You posted in VBA subforum asking for some code but then you say "no macros". This requires macros.

    You would use the Worksheet_Change event sub in the module for the sheet containing this data. However, the way I would do this is by addressing the design of your data, and use formulas instead of VBA. Also, it is not good design to cram two pieces of data into one cell as text. You should have a start time in one cell and end time in another cell, using Excel time values.

    Please attach a file and I can show you solutions that work both ways.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Expert
    Join Date
    08-17-2007
    Location
    Poland
    Posts
    2,207

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Below, all I can do for you without macros.
    Apply custom cell formatting.
    Please Login or Register  to view this content.
    If you type 1 you get "7:00/15:30", if you type 2 you get "13:30/22:00". For any other positive numeric value you will get "22:00/6:00".

    Artik

  5. #5
    Registered User
    Join Date
    11-05-2016
    Location
    Constanta
    MS-Off Ver
    2010
    Posts
    13

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Quote Originally Posted by 6StringJazzer View Post
    I am a little confused here. You posted in VBA subforum asking for some code but then you say "no macros". This requires macros.

    You would use the Worksheet_Change event sub in the module for the sheet containing this data. However, the way I would do this is by addressing the design of your data, and use formulas instead of VBA. Also, it is not good design to cram two pieces of data into one cell as text. You should have a start time in one cell and end time in another cell, using Excel time values.

    Please attach a file and I can show you solutions that work both ways.
    I attached the example.
    Attached Files Attached Files

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Here is a VBA solution and a redesign.

    I assumed that 13:30 was an error in your description and should be 15:30.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    11-05-2016
    Location
    Constanta
    MS-Off Ver
    2010
    Posts
    13

    Re: VBA code to replace "1" with "7:00/ 15:30"

    The VBA example is almost perfect, with some mentions:
    1. I dont need the message telling me it didnt find the shift. I tried to remove that If, but... no success. I messed everything up.
    2. When i clear the content of the cell it automatically adds "/". Can you remove that also, please ?
    3. I need to apply this code to a range, from C1 to AG400 (or to AG80, depending on needs). Otherwise, when i enter 1, 2, or 3 on the Crt. No on A column it also changes the content.

    Many Thanks !

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,705

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Since you were asking for VBA code, hopefully you can take the work I've done so far for free and tailor it to your specific needs.

  9. #9
    Registered User
    Join Date
    11-05-2016
    Location
    Constanta
    MS-Off Ver
    2010
    Posts
    13

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Quote Originally Posted by 6StringJazzer View Post
    Since you were asking for VBA code, hopefully you can take the work I've done so far for free and tailor it to your specific needs.
    I really appreciate your help and didn't mean to be ungrateful. I only asked those modification because i practically know nothing about coding.
    Thank you once again! I hope i will manage to fix it.

  10. #10
    Registered User
    Join Date
    11-05-2016
    Location
    Constanta
    MS-Off Ver
    2010
    Posts
    13

    Re: VBA code to replace "1" with "7:00/ 15:30"

    Quote Originally Posted by GLV View Post
    I really appreciate your help and didn't mean to be ungrateful. I only asked those modification because i practically know nothing about coding.
    Thank you once again! I hope i will manage to fix it.
    My apologies!
    This is the another post:
    https://www.excelforum.com/excel-pro...ml#post5806823

+ 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. How to nest an "if", "substitute", "find and replace" commands
    By deeqson12 in forum Hello..Introduce yourself
    Replies: 2
    Last Post: 04-15-2020, 06:15 PM
  2. Replies: 5
    Last Post: 02-05-2019, 12:03 AM
  3. Replies: 3
    Last Post: 05-02-2018, 08:03 AM
  4. [SOLVED] Column X-Ref list - Sheet1 Col A "pages", Col B:FL "Req" to Sheet2 ColA "req", ColB "page"
    By excel-card-pulled in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 04-07-2017, 09:30 AM
  5. [SOLVED] VBA help needed to remove all "/" then replace with "-" from cell "B3"and "B5"
    By krjoshi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-17-2014, 02:11 PM
  6. Replies: 4
    Last Post: 11-17-2013, 12:05 PM
  7. [SOLVED] How to USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM

Tags for this Thread

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