所以我试图按升序对列进行排序(列将根据之前在代码中定义的整数更改)所以我不想使用Range("B34:B57") etc.
相反,我在前面的代码中将有问题的列定义为变量currentcol
,下面是到目前为止的内容:
Dim firstpy As Long
Dim lastpy As Long
firstpy = Cells(34, currentcol)
lastpy = Cells(57, currentcol)
Range(firstpy & lastpy).Sort Key1:=Cells(34, currentcol), Order1:=xlAscending, Header:=xlNo
我相信问题是在Key1
参考?有人能帮忙解决这个问题吗?:)
对单列范围进行排序
快速修复
Option Explicit
Sub Test()
Const CurrentCol As Long = 1 ' just to make it compile
Dim ws As Worksheet: Set ws = ActiveSheet ' improve!
Dim firstPy As Range: Set firstPy = ws.Cells(34, CurrentCol)
Dim lastPy As Range: Set lastPy = ws.Cells(57, CurrentCol)
Dim rg As Range: Set rg = ws.Range(firstPy, lastPy)
' Or without the variables:
'Set rg = ws.Range(ws.Cells(34, CurrentCol), ws.Cells(57, CurrentCol))
rg.Sort Key1:=rg, Order1:=xlAscending, Header:=xlNo
End Sub