Consulting

Results 1 to 19 of 19

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

  1. #1
    VBAX Regular
    Joined
    Mar 2023
    Posts
    13
    Location

    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. But with no macros.
    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 !

  2. #2
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,191
    Location
    Welcome to the forum,

    What version of Excel do you use?

    Also a little confused, in the title you state: "VBA Code to replace"
    But then in the main text you state: "But with no macros."
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Can you attach a sample workbook so we can understand how and where this procedure is meant to take place?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,191
    Location
    Also if you read the forum rules, i believe you should provide a link when cross posting:
    https://www.excelforum.com/excel-pro...0-15-30-a.html
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  5. #5
    VBAX Regular
    Joined
    Mar 2023
    Posts
    13
    Location
    As i said, i am newbie. Barely know a thing or two about VBA.
    I attached the spreadsheet in which i need this replacement to take place.
    Basically, when i type 1 and press enter, i need it to automatically change into "7:00/ 15:30".
    Many thanks !

    Quote Originally Posted by georgiboy View Post
    Welcome to the forum,

    What version of Excel do you use?

    Also a little confused, in the title you state: "VBA Code to replace"
    But then in the main text you state: "But with no macros."
    Microsoft® Excel® for Microsoft 365 MSO (Version 2302 Build 16.0.16130.20332) 64-bit
    Attached Files Attached Files

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Check out the attached; no macro/vba.
    Attached Files Attached Files
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Regular
    Joined
    Mar 2023
    Posts
    13
    Location
    Quote Originally Posted by p45cal View Post
    Check out the attached; no macro/vba.
    The problem i encountered with Custom formatting is that the content can't be displayed if the column has a width of 3.14 (the width i need).

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by GLV View Post
    The problem i encountered with Custom formatting is that the content can't be displayed if the column has a width of 3.14 (the width i need).
    I noticed this; even with a linefeed in the formatting (a) autowidth goes to a width as if there was no linefeed and (b) anything less than that width shows ####.
    I don't know a work round for that except to use a macro/vba.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    Moderator VBAX Master georgiboy's Avatar
    Joined
    Mar 2008
    Location
    Kent, England
    Posts
    1,191
    Location
    You could have a key at the side and use, days, lates and nights or even D, L and N. If the times of the shifts are fixed do they need to be in every cell?
    Click here for a guide on how to add code tags
    Click here for a guide on how to mark a thread as solved

    Excel 365, Version 2403, Build 17425.20146

  10. #10
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Welcome to the forum GLV. Since it's been brought to your attention on both forums, and noticeably you not reacted on either forum re the cross posting issue, I shall bring it to your attention. Cross posting ( that is posting the same issue on another forum) breeches the rules of almost all vba forums that I'm aware of. Please show some respect to those members who volunteer their time to assist. if you are getting some assistance elsewhere then allow us to view that help as it could easily enable us to help you better.

    I need a response from you here.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  11. #11
    VBAX Regular
    Joined
    Mar 2023
    Posts
    13
    Location
    My apologies!
    This is the another post:
    https://www.excelforum.com/excel-pro...ml#post5806823

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,054
    Location
    Okay, thank you for acknowledging the other postings.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  13. #13
    try this,
    type 1 and it will return: 7:00/ 15:30
    type 2 and it will return: 22/ 6:30
    Attached Files Attached Files

  14. #14
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    This will do the 1,2,3 conversion, but the tiny column width is a problem for the longer strings

    Capture.JPG

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim r As Range
        Dim iLastRow As Long, iLastCol As Long
    
    
        Set r = Target.Cells(1, 1)
        
        If Len(r.Value) = 0 Then Exit Sub
        
        With Me
            iLastRow = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
            iLastCol = .Cells(2, .Columns.Count).End(xlToLeft).Column
        End With
        
        With r
            If .Row < 4 Or .Row > iLastRow Then Exit Sub
            If .Column < 3 Or .Column > iLastCol Then Exit Sub
            
            Application.EnableEvents = False
            Select Case .Value      '   1st is from     7 to 15:30, 2nd is from 13:30 to 22:00 and 3rd is from 22:00 to 06:30.
                Case 1
                    .Value = "7:00/15:30"
                Case 2
                    .Value = "13:30/22:00"
                Case 3
                    .Value = "22:00/06:30"
            End Select
            Application.EnableEvents = True
        End With
    
    End Sub
    Attached Files Attached Files
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  15. #15
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by Paul_Hossler View Post
    the tiny column width is a problem for the longer strings
    Perhaps a variation along the lines of?:
                    .Value = "7:00/" & vblf & "15:30"
    (untested)
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  16. #16
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,724
    Location
    @p45cal -- I did tried that but still didn't fit
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  17. #17
    VBAX Expert Dave's Avatar
    Joined
    Mar 2005
    Posts
    835
    Location
    Maybe...
    r.ShrinkToFit = True
    Too tiny to see? Dave

  18. #18
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,873
    Quote Originally Posted by Paul_Hossler View Post
    @p45cal -- I did tried that but still didn't fit
    It could be down to zoom levels. I tested my suggestion, no changes to column width or font size, then at zoom 100% I got:

    2023-03-27_152944.png

    and at zoom 130% (nothing else changed) got:

    2023-03-27_153013.png
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  19. #19
    VBAX Regular
    Joined
    Mar 2023
    Posts
    13
    Location
    Thanks all for your time!

    I finally used this: https://www.excelforum.com/attachmen...v-example.xlsx
    It's 6StringJazzer's solution.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •