The reason your code is throwing an error is that you are trying to assign the return value of Range.Select
to an object variable. However, the Select
method doesn't return an object, it returns True
(if the range was successfully selected).
There are two ways to achieve exactly what your code is trying to do: select the range and then assign the selection to the object variable:
ActiveWorkbook.Worksheets("HR Eval Report").Range("BA8").Select
ActiveWorkbook.Worksheets("HR Eval Report").Range(Selection, _
Selection.End(xlToRight)).Select
Set rngOutData = Selection
or assign the range to the variable and then select that:
ActiveWorkbook.Worksheets("HR Eval Report").Range("BA8").Select
Set rngOutData = ActiveWorkbook.Worksheets("HR Eval Report").Range(Selection, _
Selection.End(xlToRight))
rngOutData.Select
However almost certainly there are much better ways to achieve the overall objective of sorting the cells, and you should generally avoid the use of Select
(see this question for more). To get the same range into rngOutData
for instance, the following is faster, more robust and easier to read:
With ActiveWorkbook.Worksheets("HR Eval Report")
Set rngOutData = .Range(.Range("BA8"), .Range("BA8").End(xlToRight))
End With
A similar thing applies to ActiveWorkbook
- generally it would be better to have a variable containing a reference to the workbook and use that.