Consulting

Results 1 to 3 of 3

Thread: Border Row Macro problem

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Border Row Macro problem

    I have a macro that borders an entire row (up to the last column in 1:1 that has data) if any value is entered in column B. And it is set so that if a value is deleted from column B, that the border would be removed.

    My problem is that if the user deletes two values (or more) at the same time (i.e. highlighted, then deleted) then I get an error with the code. Take a look:


    Private Sub Worksheet_Change(ByVal Target As Range)
    'Borders the previous row if a value is entered in the B:B column
    Dim LastCol As Long
    If Target.Column = 2 Then
    LastCol = Range("IV" & 1).End(xlToLeft).Column - 1
    LastCol = IIf(LastCol < 0, 0, LastCol)
    If Target.Value = Empty Then
    Range(Target.Offset(0, -1), Target.Offset(0, LastCol - 1)) _
    .Borders.LineStyle = xlNone
    Else
    Range(Target.Offset(0, -1), Target.Offset(0, LastCol - 1)) _
    .Borders.Weight = xlThin
    End If
    End If
    End Sub
    The red text is where I get the error. I think the same would happen if the user tries to copy two values (or more) into column B.

    Anybody have an idea how to avoid this???




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    Administrator
    Chat VP VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    one simple addition fixes this prob...

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    'Borders the previous row if a value is entered in the B:B column
          Dim LastCol As Long
          If Target.Column = 2 Then
                LastCol = Range("IV" & 1).End(xlToLeft).Column - 1
                LastCol = IIf(LastCol < 0, 0, LastCol)
                On Error Resume Next
                If Target = Empty Then
                      Range(Target.Offset(0, -1), Target.Offset(0, LastCol - 1)) _
                                  .Borders.LineStyle = xlNone
                Else
                      Range(Target.Offset(0, -1), Target.Offset(0, LastCol - 1)) _
                                  .Borders.Weight = xlThin
                End If
          End If
    End Sub
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Awesome! That's that fastest "solved" I've ever seen!
    Thanks Johnske!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

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