将值从A到Z排序

时间:2018-08-24 13:15:52

标签: excel vba excel-vba sorting

嗨,我创建了一个将值添加到特定范围的代码。这些范围已经具有允许范围变为动态的公式。

我想要做的就是对这些命名范围A到Z之一进行排序,这就是“名称”。

我遇到的问题是,它似乎在排序,但实际上并未对任何值进行排序。有人可以帮忙吗?

下面是我的代码

Private Sub CommandButton1_Click()

Dim LRow As Long
Dim BRow As Long

Dim ws6 As Worksheet

Application.ScreenUpdating = False

Set ws6 = Worksheets("Lookup Vals")

    LRow = ws6.Range("C:C").Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    ws6.Cells(LRow, 3).Value = Me.tbLNName.Value

    Select Case True
    Case Me.CBLNComp.Value = "Type1": BRow = ws6.Range("L:L").Find(What:="*", SearchOrder:=xlRows, _
                                                SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
        ws6.Cells(BRow, 12).Value = Me.tbLNName.Value
        ws6.Cells(BRow, 13).Value = Me.tbLNBP.Value
        ws6.Cells(BRow, 14).Value = Me.CBLNComp.Value

    Case Me.CBLNComp.Value = "Type2": BRow = ws6.Range("P:P").Find(What:="*", SearchOrder:=xlRows, _
                                                SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
        ws6.Cells(BRow, 16).Value = Me.tbLNName.Value
        ws6.Cells(BRow, 17).Value = Me.tbLNBP.Value
        ws6.Cells(BRow, 18).Value = Me.CBLNComp.Value

    End Select

Application.ScreenUpdating = True

With ActiveWorkbook.Worksheets("Lookup Vals").AutoFilter.Sort
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With

Unload AddBP

End Sub

1 个答案:

答案 0 :(得分:1)

我相信以下方法可以解决问题,您未能说明要应用排序的范围:

Private Sub CommandButton1_Click()
Dim LRow As Long
Dim BRow As Long
Dim ws6 As Worksheet: Set ws6 = ThisWorkbook.Worksheets("Lookup Vals")

Application.ScreenUpdating = False

    LRow = ws6.Range("C:C").Find(What:="*", SearchOrder:=xlRows, _
        SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

    ws6.Cells(LRow, 3).Value = Me.tbLNName.Value

    Select Case True
    Case Me.CBLNComp.Value = "Type1": BRow = ws6.Range("L:L").Find(What:="*", SearchOrder:=xlRows, _
                                                SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
        ws6.Cells(BRow, 12).Value = Me.tbLNName.Value
        ws6.Cells(BRow, 13).Value = Me.tbLNBP.Value
        ws6.Cells(BRow, 14).Value = Me.CBLNComp.Value

    Case Me.CBLNComp.Value = "Type2": BRow = ws6.Range("P:P").Find(What:="*", SearchOrder:=xlRows, _
                                                SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
        ws6.Cells(BRow, 16).Value = Me.tbLNName.Value
        ws6.Cells(BRow, 17).Value = Me.tbLNBP.Value
        ws6.Cells(BRow, 18).Value = Me.CBLNComp.Value

    End Select

Application.ScreenUpdating = True

    ws6.Sort.SortFields.Clear
    ws6.Sort.SortFields.Add Key:=Range("Name"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    With ws6.Sort
        .SetRange Range("Name")
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
Unload AddBP
End Sub