Sorting Worksheet data by column values using Excel VBA

僤鯓⒐⒋嵵緔 提交于 2019-12-04 03:15:00

问题


I have next userform developed in vba, which takes info from a worksheet for displaying info

I want to order all the info aphabetically by a Segment, this is the code:

Function llenarDatosTabla()

    Dim vList As Variant
    Dim ws As Worksheet: Set ws = Worksheets(BD_PRODXSIST)

    ListBox1.Clear

    With ws
        If (IsEmpty(.Range("AA2").Value) = False) Then

            Dim ultimoRenglon As Long: ultimoRenglon = devolverUltimoRenglonDeColumna("A1", BD_PRODXSIST)

            vList = ws.Range("AA2:AA" & ultimoRenglon & ":AL2").Value

            If IsArray(vList) Then
                Me.ListBox1.List = vList
            Else
                Me.ListBox1.AddItem (vList)
            End If

        End If

        Me.ListBox1.ListIndex = -1

    End With




    Set vList = Nothing
    Set ws = Nothing
End Function

how to make it ordered by 'AD' (SEGMENTO) column???


回答1:


You can sort your Excel Worksheet in ascending order using VBA statement like the following:

Columns("A:XFD").Sort key1:=Range("AD:AD"), order1:=xlAscending, Header:=xlYes

Note: in the column range Columns("A:XFD") instead of XFD enter the last used column pertinent to your case, e.g. Columns("A:DD").

Hope this will help.




回答2:


To sort a data table, use Excel Names in conjunction with the CurrentRegion function. This is less risky than hard-coding column references and can be done in two simple steps.

The reason it's preferable to specifying columns is that if you get the columns wrong or they change later, you'll scramble your data! When you perform the sort, the cells in any omitted column(s) will remain where they are, becoming part of the wrong rows. And this is exactly what will happen if you add further columns later, unless you remember to update your VBA.

Here are the two simple steps for using this approach. For this example, I've chosen a data table with four columns and four rows:

We are going to sort by COL3 descending. The cells in the other three columns share identical values, enabling us to readily verify they all stay with the correct rows.

Step 1: choose a cell in the data table that's unlikely to ever be removed, such as the header of a column you intend to make permanent, and define a Name for this cell. You can define the name by selecting the cell and typing directly in Excel's Name dropdown above the worksheet. Here I've used the name RegionTag:

Straight away, CurrentRegion can reference the whole data table just from this. You can see it in action if you code a line of VBA to select the table:

Range("RegionTag").CurrentRegion.Select

This is the result:

That's just for illustration, showing the power of the Name/CurrentRegion combination. We don't need to select the table in order to sort it.

Step 2: define a second Name, this time for the column you want to sort by:

Make sure the Name refers to the entire column, selected by clicking the column header, rather than just a range of cells in the column.

That's it! With these two Names defined, we can sort the data table without concerning ourselves with its rows and columns, even if more are added later:

Range("RegionTag").CurrentRegion.Sort _
    key1:=Range("SortCol"), order1:=xlDescending, Header:=xlYes

Here is our data table sorted using the above statement:



来源:https://stackoverflow.com/questions/27571923/sorting-worksheet-data-by-column-values-using-excel-vba

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!