从当前工作簿中,我想在源工作簿中打开一个工作表,关闭可能存在的任何过滤,按一列排序,然后是另一列,然后是另一列。我收到 1004 和其他错误。这需要在 2003 年运行。任何帮助将不胜感激!
Dim WB As Workbook, WasWBOpen As Boolean, srcfile As String, srcpath As String,
onecol as integer, twocol as integer, thrcol as integer
srcpath = "blahblah"
srcfile = "blah.xls"
On Error Resume Next
Set WB = Workbooks(srcfile)
WasWBOpen = True
On Error GoTo 0
If WB Is Nothing Then
Set WB = Workbooks.Open(srcpath & srcfile, UpdateLinks:=False)
WasWBOpen = False
End If
'code before this opens source wkbook
lstrow = Worksheets("Sheet1").UsedRange.Row - 1 + Worksheets("Sheet1").UsedRange.Rows.Count
lstcol = Worksheets("Sheet1").UsedRange.Column - 1 + Worksheets("Sheet1").UsedRange.Columns.Count
onecol=3
twocol=5
thrcol=8
With WB.Sheets("Sheet1")
.AutoFilterMode = False
.Range("1:1").AutoFilter
'Here's where error occurs--
.Range(Cells(1, 1), Cells(lstrow, lstcol)).Sort _
Key1:=Columns(onecol), Order1:=xlAscending, _
Key2:=Columns(twocol), Order2:=xlAscending, _
Key3:=Columns(thrcol), Order3:=xlAscending, Header:=xlYes
End With
If WasWBOpen = False Then
WB.Close
End If
最佳答案
.Range(Cells(1, 1), Cells(lstrow, lstcol)).Sort _
最好写成:
.Range(.Cells(1, 1), .Cells(lstrow, lstcol)).Sort _
关于vba - Excel 2003 : Programmatic sort of sheet in different workbook,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8791760/