简体   繁体   中英

Sorting Worksheet data by column values using Excel VBA

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???

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, eg Columns("A:DD") .

Hope this will help.

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:

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM