如何对最后一行和最后一列未知的另一个工作表进行排序?我已经处理了以下代码,但不幸的是,该代码无法正常工作。
Sub sample()
Dim ws As Worksheet
Dim lRow As Long, lCol As Long
Dim rng As Range
Set ws = Workbooks("x.xlsx").Sheets(1)
With ws
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
lCol = .Cells(1, .Columns.Count).End(xlToLeft).Column
Set rng = .Range(.Cells(lRow, 1), .Cells(lRow, lCol))
rng.Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlNo
'
End With
End Sub
答案 0 :(得分:0)
问题出在这一行:
Set rng = .Range(.Cells(lRow, 1), .Cells(lRow, lCol))
在这种情况下,您的范围仅一行,因为.Cells
中的两个参数均为lRow
。如果标题位于第一行,则应该为:
Set rng = .Range(.Cells(1, 1), .Cells(lRow, lCol))