Consulting

Results 1 to 8 of 8

Thread: Reposition cursor in cell that contains the error

  1. #1
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location

    Reposition cursor in cell that contains the error

    I use the following macro to format all cells in a column that should contain only zipcodes. If the cell is blank, contains a space, or contains a non-numeric character, the macro errors out and a message is given. Is there a way to modify this macro so that it will reposition the cursor in the cell that contained the error? There are sometimes thousands of rows in the spreadsheet, so it would be great if the macro could go right to the cell that has a problem.

    Sub FormatZipcodes()
    
     
    ' Set up the error trap
    On Error GoTo ErrHandle
    
    For Each xCell In Selection
        xCell.Value = CDec(xCell.Value)
    Next xCell
    Selection.NumberFormat = "General"
    With Selection
        .HorizontalAlignment = xlRight
        .VerticalAlignment = xlBottom
        .WrapText = True
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
    
    Exit Sub
    
    ErrHandle:
        MsgBox Prompt:="An error occurred. Check zipcodes for hyphens, spaces, and alpha characters.", _
        Title:="Formatting Error"
        End
    End Sub
    Thanks for your help!

    Cheryl

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    is this what you mean?
    Sub FormatZipcodes()
         
         
         ' Set up the error trap
        On Error GoTo ErrHandle
         
        For Each xcell In Selection
            xcell.Value = CDec(xcell.Value)
        Next xcell
        Selection.NumberFormat = "General"
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
         
        Exit Sub
         
    ErrHandle:
        MsgBox Prompt:="An error occurred. Check zipcodes for hyphens, spaces, and alpha characters.", _
        Title:="Formatting Error"
        xcell.Select  ' <------
        End
    End Sub

  3. #3
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Exactly what I needed! Thank you so much!

    Cheryl

  4. #4
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Is it possible to add a message at the end of the macro that shows the range of cells that were formatted? If an error occurs, could the message indicate which cell contained the error?

    Cheryl

  5. #5
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Is this what you want?
    Sub FormatZipcodes()
        Dim ErrorCells As Range
        
        ' Set up the error trap
        On Error GoTo ErrHandle
        
        For Each xcell In Selection
            xcell.Value = CDec(xcell.Value)
        Next xcell
        Selection.NumberFormat = "General"
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        ErrorCells.Select
        Exit Sub
        
    ErrHandle:
        If ErrorCells Is Nothing Then
            Set ErrorCells = xcell
        Else
            Set ErrorCells = Union(ErrorCells, xcell)
        End If
        Resume Next
    End Sub

  6. #6
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    I updated the routine, you may like it better:
    Sub FormatZipcodes()
        Dim ErrorCells As Range
        Dim ErrorFound As Boolean
        
         ' Set up the error trap
        On Error GoTo ErrHandle
         
        For Each xcell In Selection
            xcell.Value = CDec(xcell.Value)
        Next xcell
        Selection.NumberFormat = "General"
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        If ErrorFound Then
            MsgBox "Errors found"
            With ErrorCells
                .Select
                .Interior.ColorIndex = 3
            End With
        End If
        Exit Sub
         
    ErrHandle:
        ErrorFound = True
        If ErrorCells Is Nothing Then
            Set ErrorCells = xcell
        Else
            Set ErrorCells = Union(ErrorCells, xcell)
        End If
        Resume Next
    End Sub

  7. #7
    VBAX Mentor clhare's Avatar
    Joined
    Mar 2005
    Posts
    470
    Location
    Wow! I do like that! The only issue I see is that the empty cell did not come up as an error. Instead it put a "0" in there. I want it to stay empty and also be highlighted. Is that possible?

  8. #8
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Well, your routine did not handle blanks.... Here is an updated version to handle it
    Sub FormatZipcodes()
        Dim ErrorCells As Range
        Dim ErrorFound As Boolean
         
         ' Set up the error trap
        On Error GoTo ErrHandle
         
        For Each xcell In Selection
            If xcell = "" Then
                ErrorFound = True
                If ErrorCells Is Nothing Then
                    Set ErrorCells = xcell
                Else
                    Set ErrorCells = Union(ErrorCells, xcell)
                End If
            Else
                xcell.Value = CDec(xcell.Value)
            End If
        Next xcell
        Selection.NumberFormat = "General"
        With Selection
            .HorizontalAlignment = xlRight
            .VerticalAlignment = xlBottom
            .WrapText = True
            .Orientation = 0
            .AddIndent = False
            .IndentLevel = 0
            .ShrinkToFit = False
            .ReadingOrder = xlContext
            .MergeCells = False
        End With
        If ErrorFound Then
            MsgBox "Errors found"
            With ErrorCells
                .Select
                .Interior.ColorIndex = 3
            End With
        End If
        Exit Sub
         
    ErrHandle:
        ErrorFound = True
        If ErrorCells Is Nothing Then
            Set ErrorCells = xcell
        Else
            Set ErrorCells = Union(ErrorCells, xcell)
        End If
        Resume Next
    End Sub

Posting Permissions

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