vba - Excel 2003 : Programmatic sort of sheet in different workbook

标签 vba excel excel-2003

从当前工作簿中,我想在源工作簿中打开一个工作表,关闭可能存在的任何过滤,按一列排序,然后是另一列,然后是另一列。我收到 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/

相关文章:

excel - 如何在vba中的字符串中加上双引号?

vba - 将变体数组转储到范围 - VBA excel 错误 1004

excel - 将两个字符对象添加在一起,以连接它们的文本,但保留每个字符的格式

excel - 停止 VBA Evaluate 调用目标函数两次

excel - 基于行字段的数据透视图系列颜色

python - 字符串分割对单个字符串有效,但对 pandas 中的一系列字符串无效

c# - 如何复制由 AutoFilter() 方法过滤的行

excel - 如何将 ms excel 2003 链接到 ms access 2003

VBA代码速度慢,需要优化

excel - 将单元格字符串拆分为单个单元格