Vba 将值A排序到Z
嗨,我已经创建了一个代码,它可以为某些范围添加值。这些范围已经有了一个公式,允许范围变得动态 我想做的是对这些命名范围A到Z中的一个进行排序,称为Name 我遇到的问题是,它似乎对任何值进行排序,但实际上并没有对任何值进行排序。有人能帮忙吗 下面是我的代码Vba 将值A排序到Z,vba,excel,sorting,Vba,Excel,Sorting,嗨,我已经创建了一个代码,它可以为某些范围添加值。这些范围已经有了一个公式,允许范围变得动态 我想做的是对这些命名范围A到Z中的一个进行排序,称为Name 我遇到的问题是,它似乎对任何值进行排序,但实际上并没有对任何值进行排序。有人能帮忙吗 下面是我的代码 Private Sub CommandButton1_Click() Dim LRow As Long Dim BRow As Long Dim ws6 As Worksheet Application.ScreenUpdating =
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
我相信下面应该可以做到这一点,您没有说明您希望排序适用的范围:
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
谢谢Xabier。非常感谢。工作很愉快