+ Reply to Thread
Results 1 to 22 of 22

Set Cell To Display Number(s) Entered In Four-Digit Format

Hybrid View

  1. #1
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Question Set Cell To Display Number(s) Entered In Four-Digit Format

    Hi everyone,

    I need to set up Cell C4 on my sheet to convert data entered to this format:

    If a 1 is entered, it would convert to 0010.
    A 9 would equal 0090.
    15 = 0150
    46 = 0460
    320 = 3200
    9-46 = 0090-0460
    15-320 = 0150-3200
    0700 = 0700

    As you can see, it basically needs to convert any three-digit number or less to a four-digit format, with a 0 at the end and 0's at the front as required.

    Now, I have this half-way working with this formula which romperstomper helped me with here.

    For Each rngCell In Intersect(Target, rngMonitor).Cells
                If Val(rngCell.Text) > 0 Then
                    If Len(rngCell.Text) < 4 Then
                        rngCell.Value = Right("000" & rngCell.Text, 3) & "0"
                    ElseIf Len(rngCell.Text) > 4 Then
                        MsgBox "Entry is too long!"
                        rngCell.ClearContents
                    End If
                End If
            Next rngCell
    This has been working great if only one number was entered, but now I've come across the problem where sometimes a '-' will need to be used to specify a range of numbers, such as 0090-0460. How could this current code be altered to check each 'group' of numbers, and if they are not already entered in the correct format, change them to the four-digit format as shown above?
    There is so much good in the worst of us,
    And so much bad in the best of us,
    That it hardly behooves any of us
    To talk about the rest of us.

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Maybe try this:

    Dim D%
    
    For Each rngcell In Intersect(Target, rngMonitor).Cells
        If Val(rngcell.Text) > 0 Then
            D = InStr(1, rngcell.Text, "-", vbTextCompare)
            If D > 0 Then
                rngcell.Text = Format(Left(rngcell.Text, D - 1), "000") & "0-" & Format(Right(rngcell.Text, D - 1), "000") & "0"
            ElseIf Len(rngcell.Text) < 4 Then
                rngcell.Text = Right("000" & rngcell.Text, 3) & "0"
            Else
                MsgBox "Entry is too long!"
                rngcell.ClearContents
            End If
        End If
    Next
    Last edited by abousetta; 06-28-2012 at 07:14 AM. Reason: Corrected syntax
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Thank you so much for your reply! Any idea why I'm getting an 'Object Required' error when I try to enter a value in Cell C4?

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Which line of code is giving you the error. I wrote it from scratch using different object names and then converted it to fit with your code so it might need a slight tweak.

  5. #5
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    I'm not even sure, the message just pops up, I hit OK, and I'm not taken to the code.

    Here is my entire code for that sheet, as it was before adding yours:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim rngCell As Range
        Dim rngMonitor As Range
        On Error GoTo err_handle
    
        'If 'Entry Page'!F7 changes:
        If Not Intersect(Target, Me.Range("$F$7")) Is Nothing Then
            Application.EnableEvents = False
            Select Case Len(Trim(Target.Text))
                Case 0:         Sheets("Inspection Report").Range("L6, L86, L166").Value = ""
                                Sheets("Inspection Report").Range("C77, C157, C237").Value = ""
                
                Case Is <= 21:  Sheets("Inspection Report").Range("L6, L86, L166").Value = Target.Text
                                Sheets("Inspection Report").Range("C77, C157, C237").Value = ""
                
                Case Else:      Sheets("Inspection Report").Range("L6, L86, L166").Value = "See Notes"
                                Sheets("Inspection Report").Range("C77, C157, C237").Value = Target.Text
            End Select
            Application.EnableEvents = True
        End If
    
           If Target.Address = "$F$22" Then
            If Target.Value > 0 Then Exit Sub
            Application.EnableEvents = False
            If Target.Value = "" Then MsgBox "You need to print at least 1 label!"
            Target.Value = 1
            Application.EnableEvents = True
        End If
    
        ' adjust to whatever cells you want to monitor
        Set rngMonitor = Range("C4")
    
        If Not IsValidFileName(ThisWorkbook.Name) Then
            MsgBox "Please save the file as <partnumber>Rev<revision number> before entering data!"
            On Error Resume Next
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
            Exit Sub
        End If
    
        If Not Intersect(Target, Range("C8,C12")) Is Nothing Then
            MsgBox "Do not change the part number or revision number. Save the file as the relevant name and the numbers will change automatically."
            On Error Resume Next
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
            Exit Sub
        End If
    
        If Not Intersect(Target, rngMonitor) Is Nothing Then
    
            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With
    
            For Each rngCell In Intersect(Target, rngMonitor).Cells
                If Val(rngCell.Text) > 0 Then
                    If Len(rngCell.Text) < 4 Then
                        rngCell.Value = Right("000" & rngCell.Text, 3) & "0"
                    ElseIf Len(rngCell.Text) > 4 Then
                        MsgBox "Entry is too long!"
                        rngCell.ClearContents
                    End If
                End If
            Next rngCell
    
        End If
    
        If Not Intersect(Target, Range("NameCells")) Is Nothing Then
            For Each rngCell In Intersect(Target, Range("NameCells"))
                UpdatePics rngCell.Offset(, -1).Name.Name, rngCell.Value
            Next rngCell
        End If
    
    clean_up:
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
        Exit Sub
    
    err_handle:
        MsgBox Err.Description
        Resume clean_up
    
    End Sub

  6. #6
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Try removing the error handler and when you get the error, click on Debug. That will highlight the row causing the problem

  7. #7
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    How do I go about removing the error handler?

  8. #8
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Remove the line:

    On Error GoTo err_handle

  9. #9
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Alright, it's this line: rngCell.Text = Right("000" & rngCell.Text, 3) & "0"

  10. #10
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    In the original code you posted it was .value instead of .text so try that

    rngCell.Value = Right("000" & rngCell.Text, 3) & "0"

  11. #11
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    You know what? That nearly does it! The only issue I can still see is when I enter a 2-79, for example, it gets converted to '0020-0090' for some reason - any idea why?

  12. #12
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    From your examples, that's what I thought you wanted the output to be. What should the result be?

  13. #13
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    It's close, but if I enter 2-79, I should be getting a 0020-0790. Instead, I'm getting 0020-0090.

  14. #14
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Don't have excel in front of me right now but try this

    change:

    Format(Right(rngcell.Text, D - 1), "000")
    to:
    Format(Right(rngcell.Text, D), "000")

  15. #15
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Thanks, but that's still not working - I tried replacing just one of them to that, then the other, then both, but I'm getting all kinds of weird results back.

  16. #16
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    This is working for me. If you are still having problems then upload a workbook so I can properly debug.

    For Each rngcell In Intersect(Target, rngMonitor).Cells
        If Val(rngcell.Text) > 0 Then
            D = InStr(1, rngcell.Text, "-", vbTextCompare)
            If D > 0 Then rngcell.Value = Format(Left(rngcell.Text, D - 1), "000") & "0-" & Format(Right(rngcell.Text, D), "000") & "0"
            ElseIf Len(rngcell.Text) < 4 Then rngcell.Text = Right("000" & rngcell.Text, 3) & "0"
            Else
                MsgBox "Entry is too long!"
                rngcell.ClearContents
            End If
        End If
    Next

  17. #17
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Alright, thank you - here is my sample. Notice I tried to use your latest code, but I deleted an 'End If' as it was giving me an error. And now, entering a 4 doesn't change to 0040; entering 22-55 turns into 0220--0550 which is one too many dashes in there. Entering 2-79 returns 0020-0790, which is perfect.

    PS You'll notice an error upon opening the workbook - just click End, that's due to the fact that this is a sample workbook with sheets missing, but it won't interfere with what we're doing here.
    Attached Files Attached Files

  18. #18
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    This should work now:

        If Not Intersect(Target, rngMonitor) Is Nothing Then
            With Application
                .ScreenUpdating = False
                .EnableEvents = False
            End With
            For Each rngCell In Intersect(Target, rngMonitor).Cells
                If Val(rngCell.Text) > 0 Then
                    D = InStr(1, rngCell.Text, "-", vbTextCompare)
                    If D > 0 Then
                        rngCell.Value = Format(Left(rngCell.Text, D - 1), "000") & "0" & Format(Right(rngCell.Text, D), "000") & "0"
                    ElseIf Len(rngCell.Text) < 4 Then
                        rngCell.Value = Right("000" & rngCell.Text, 3) & "0"
                    Else
                        MsgBox "Entry is too long!"
                        rngCell.ClearContents
                    End If
                End If
            Next
        End If
    Attached Files Attached Files

  19. #19
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Thanks again! We've almost got it!

    This works!:
    2-5 = 0020-0050
    22-55 = 0220-0550
    222-555 = 2220-5550

    This doesn't:
    2-55 = 00200550
    2-555 = 00200550

    It seems to get hung up when the number of digits in each group aren't the same..

  20. #20
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Yes, I see my mistake. It should be:

    rngCell.Value = Format(Left(rngCell.Text, D - 1) * 10, "0000") & "-" & Format(Right(rngCell.Text, Len(rngCell.Text) - D) * 10, "0000")
    instead of:

    rngCell.Value = Format(Left(rngCell.Text, D - 1), "000") & "0" & Format(Right(rngCell.Text, D), "000") & "0"

  21. #21
    Forum Contributor swordswinger710's Avatar
    Join Date
    02-20-2008
    Location
    Bright, Canada
    MS-Off Ver
    2010
    Posts
    845

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    That's it! Thank you so very much!

  22. #22
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Set Cell To Display Number(s) Entered In Four-Digit Format

    Glad it worked out. If you are satisfied with the responses then please mark the thread as solved.

    Good luck.

    abousetta

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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