Consulting

Results 1 to 8 of 8

Thread: Sort data Only A2 to LastCell

  1. #1

    Sort data Only A2 to LastCell

    Hellow everybody!

    i need sort my table (column A to D), but sort first (A2) to Lastcell with data.

    Private Sub Worksheet_Change(ByVal Target As Range)'I try sort A2 to Last cell popupalizaded
        LR = Range("A" & Rows.Count).End(xlUp).Row
        Set LR = Range("A1:D" & LR)
           If Not (Application.Intersect(Worksheets(1).Range("A1:D" & LR), Target) Is Nothing) Then
             Worksheets("Plan1").Range("A1:D" & LR).Sort Key1:=Worksheets("Plan1").Range("A2"), Order1:=xlAscending
           End If
    End Sub

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    i think Worksheets(1) and Worksheets("Plan1") and the worksheet with the below event code refer to the same worksheet.

    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim rng As Range
        
        Set rng = Range("A1:D" & Range("A" & Rows.Count).End(xlUp).Row)
        If Not Intersect(rng, Target) Is Nothing Then
            rng.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes
        End If
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Thank you...

    works, but I figured it would solve, but does not solve.
    In column A has values ​​(numbers), my table has 4 columns (column A to D).


    I wish that after entering the number (column A), only after entering the cells of the columns (column A to D), is that the code will sort the data.

    Thank you!!

  4. #4
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    you are welcome. i dont understand your requirement. you mean the range between target cell's row and the last row of the table will be sorted (sort by rows)? or only target cell's row will be sorted (sort by columns)? or something else?
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  5. #5
    I'll try to explain better

    Cod Nome Value Tip
    37 MARK 321 OK
    54 JOHN 457 OK
    86 KLEBER 545 OK
    <<--before entering the code










    Cod Nome Value Tip
    1 after pressing the ENTER key
    37 MARK 321 OK
    54 JOHN 457 OK
    86 KLEBER 545 OK
    Here <<--The cursor is still here













    I have two options, only sort after data preecher data in the last cell (Column A to D).


    or go with the cursor after the data classification.
    look

    before entering the data (Column A to D)
    Cod Nome Value Tip
    37 MARK 321 OK
    54 JOHN 457 OK
    86 KLEBER 545 OK
    1 MANCUBUS 124 OK










    After the data is entered (Column A to D).
    Cod Nome Value Tip
    1 MANCUBUS 124 OK
    37 MARK 321 OK
    54 JOHN 457 OK
    86 KLEBER 545 OK

  6. #6
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    try this:
    Private Sub Worksheet_Change(ByVal Target As Range)        
    Dim rng As Range    
    Dim LastRow As Long        
    LastRow = Range("A" & Rows.Count).End(xlUp).Offset(1).Row    
    Set rng = Range("A1:D" & LastRow)        
    If Not Intersect(rng, Target) Is Nothing Then        
    If Application.CountA(Range(Cells(Target.Row, 1), Cells(Target.Row, 4))) = 4 Then            
    'to ensure values are entered in cols A to D            
    rng.Sort Key1:=Range("A2"), Order1:=xlAscending, Header:=xlYes            
    Cells(LastRow, 1).Select 'go to last blank cell in col A        
    End If    
    End If
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  7. #7
    i'm very glag, thank you!!

  8. #8
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    im glad it helped. cheers.
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

Posting Permissions

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