Ordenar un rango después de la actualización de datos en Excel

I have a table in excel that updates automatically (or manually) by pulling from a website.

I have another table that is linked, that I want to sort automatically every time the data is updated. Any way to do this?

preguntado el 09 de marzo de 12 a las 14:03

1 Respuestas

In the Sheet where the table that needs to update automatically is located, you can add something like the code below (pseudo code) - it will sort the table every time is is changed. Note that it might get slow if you get frequent updates.

Private Sub Worksheet_Change(ByVal Target As Range)
    If Target is in the table (you can check Target.Row and Target.Column for example) Then
        Sort the table
    End If
End Sub

If the range is a named range called named_range you can do this for example (not very efficient but readable):

Private Sub Worksheet_Change(ByVal Target As Range)
    For Each c In [named_range]
      If c = Target Then MsgBox "OK"
    Next c
End Sub

Or you can do this, which will perform better is the named range is large:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim row_start As Long
    Dim row_end As Long
    Dim col_start As Long
    Dim col_end As Long

    row_start = [named_range].Cells(1, 1).Row
    col_start = [named_range].Cells(1, 1).Column
    row_end = row_start + [named_range].Rows.Count - 1
    col_end = col_start + [named_range].Columns.Count - 1

    If Target.Row >= row_start And Target.Row <= row_end And Target.Column >= col_start And Target.Column <= col_end Then
      MsgBox "In range"
    End If

End Sub

respondido 09 mar '12, 15:03

Thanks for the suggestion... I have a table where the first column is Rank (using the rank function). So the rank changes based on the updated data. How can I monitor that whole range for a change (since they won't necessarily all change)? - McB

Assuming the range is A1:A5 for example, you can test: If Target.Row>=1 And Target.Row<=5 And Target.Column = 1 Then... - Assylias

How would I go about making it "IF Target IN namedRange Then" ? - McB

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.