1

people, my doubts is simple .. I created a spreadsheet in excel with several fields, eg:

NAME       ADDRESS      PHONE
carlos      ave. 1    12345678
Argeu       av .2     87654321

After this, I used the following code in the module:

Private Sub Worksheet_Activate()
ActiveSheet.ScrollArea = "$A$2:$A$300"
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column <> 1 Then Exit Sub
Dim LR As Long
LR = Cells(Rows.Count, "A").End(xlUp).Row
Range("$A$2:$A" & LR).Sort Key1:=Range("$A$2")
End Sub

This code will make in the example table, the Argeu stand on the carlos, so far so good, but I want when the Argeu is up from carlos the phone and address data also rise .. If anyone can help, thank you

Community
  • 1
  • 1

1 Answers1

0

IF you want Col C to be also sorted then include that in the sort range as well. See this.

Change

Range("$A$2:$A" & LR).Sort Key1:=Range("$A$2")

to

Range("$A$2:$C" & LR).Sort Key1:=Range("$A$2")

EDIT

Your code can be properly re-written as

Private Sub Worksheet_Change(ByVal Target As Range)
    On Error GoTo Whoa

    Application.EnableEvents = False

    '~~> For xl2007+ use .CountLarge
    If Target.Cells.CountLarge > 1 Then Exit Sub
    '~~> For xl2003 use .Count
    'If Target.Cells.Count > 1 Then Exit Sub

    Dim LR As Long

    If Application.WorksheetFunction.CountA(Cells) <> 0 Then
        LR = Cells.Find(What:="*", _
                      After:=Range("A1"), _
                      Lookat:=xlPart, _
                      LookIn:=xlFormulas, _
                      SearchOrder:=xlByRows, _
                      SearchDirection:=xlPrevious, _
                      MatchCase:=False).Row
    Else
        LR = 1
    End If

    Range("$A$2:$C" & LR).Sort Key1:=Range("$A$2")

Letscontinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume Letscontinue
End Sub

ScreenShot

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250