我在VBA中非常新,并想知道为什么用户输入的范围不适用于range()。选择以及是否有另一种解决方法。我已经记录了一个用于排序值的宏,我想只排序用户选择的值。
这是我的用户输入代码:
On Error Resume Next
Application.DisplayAlerts = False
Set DataRange = Application.InputBox(Prompt:= _
"Please select range of data.", _
Title:="specify range of data to be added", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If DataRange Is Nothing Then
Exit Sub
End If
Dim AnotherDataRange As Range
On Error Resume Next
Application.DisplayAlerts = False
Set AnotherDataRange = Application.InputBox(Prompt:= _
"Specify range.", _
Title:="specify range of req tool for recipes to be added", Type:=8)
On Error GoTo 0
Application.DisplayAlerts = True
If AnotherDataRange Is Nothing Then
Exit Sub
End If
这是分拣机的代码:
Sub sorterforrecipe()
Range(DataRange).Select 'This doesn't work
ActiveSheet.sort.SortFields.Clear
ActiveSheet.sort.SortFields.Add Key:=Range( _
AnotherDataRange), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal 'This doesn't work as well, where AnotherDataRange is yet another user input
With ActiveSheet.sort
.SetRange Range(DataRange) 'This doesn't Work
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
我想再次强调,只有用户输入的范围才会被排序。
答案 0 :(得分:0)
如果DataRange
全球,则替换:
Range(DataRange).Select
使用:
DataRange.Select