Excel2007:“;“自动化错误”;使用VBA(80010105)对数据进行排序时
我正在从Excel文件运行VBA脚本,该脚本打开另一个文件,处理数据和一些图表,然后保存它。除了我尝试对数据进行排序外,其他一切都很正常。当我到达行Excel2007:“;“自动化错误”;使用VBA(80010105)对数据进行排序时,excel,runtime-error,vba,Excel,Runtime Error,Vba,我正在从Excel文件运行VBA脚本,该脚本打开另一个文件,处理数据和一些图表,然后保存它。除了我尝试对数据进行排序外,其他一切都很正常。当我到达行.SortFields.Add Key:=Range(“J3:J11”)…时,我得到一个错误 Run-time error '-2147417851 (80010105)': Automation error The server threw an exception 我确信这与我引用Excel对象的方式有关,但我已经尝试了
.SortFields.Add Key:=Range(“J3:J11”)…
时,我得到一个错误
Run-time error '-2147417851 (80010105)':
Automation error
The server threw an exception
我确信这与我引用Excel对象的方式有关,但我已经尝试了所有方法,似乎找不到解决方案。排序代码从宏记录器借用并修改
Private Sub button1_Click()
Dim path As String
Dim exl As Excel.Application
path = ActiveWorkbook.path & "\"
Set exl = CreateObject("Excel.Application")
With exl
.Workbooks.Open path & "bin\Integrated UPSIDE with Summary.xlsm"
<...other code...>
With .Worksheets("Summary").Sort
.SortFields.Clear
.SortFields.Add Key:=Range("J3:J11") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange Range("C2:P11")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
<...other code...>
.Workbooks.Close
End With
exl.QUIT
End Sub
Private子按钮1\u Click()
将路径设置为字符串
Dim exl作为Excel.Application
path=ActiveWorkbook.path&“\”
设置exl=CreateObject(“Excel.Application”)
用exl
.Workbooks.Open path&“bin\与Summary.xlsm集成”
带.Worksheets(“Summary”).Sort
.SortFields.Clear
.SortFields.Add键:=范围(“J3:J11”)_
,SortOn:=xlSortOnValues,顺序:=xlDescending,数据选项:=xlSortNormal
.SetRange范围(“C2:P11”)
.Header=xlYes
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
.申请
以
.工作手册
以
退出
端接头
非常感谢您的任何建议!谢谢问题是您没有正确引用您的范围。您使用的排序代码是为当前Excel实例中活动工作表上的排序范围编写的
.SortFields.Add Key:=exl.Worksheets("Summary").Range("J3:J11") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange exl.Worksheets("Summary").Range("C2:P11")
解决此问题的最简单方法是引用其他Excel实例中的范围
.SortFields.Add Key:=exl.Worksheets("Summary").Range("J3:J11") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange exl.Worksheets("Summary").Range("C2:P11")
但是,我的建议是使用工作簿和工作表对象
Private Sub button1_Click()
Dim path As String
Dim exl As Excel.Application
Dim wbk As Workbook, sht As Worksheet
path = ActiveWorkbook.path & "\"
Set exl = CreateObject("Excel.Application")
With exl
Set wbk = .Workbooks.Open(path & "bin\Integrated UPSIDE with Summary.xlsm")
'<...other code...>
Set sht = wbk.Worksheets("Summary")
With sht.Sort
.SortFields.Clear
.SortFields.Add Key:=sht.Range("J3:J11") _
, SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
.SetRange sht.Range("C2:P11")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'<...other code...>
wbk.Close
Set sht = Nothing
Set wbk = Nothing
End With
exl.Quit
End Sub
Private子按钮1\u Click()
将路径设置为字符串
Dim exl作为Excel.Application
将wbk设置为工作簿,将sht设置为工作表
path=ActiveWorkbook.path&“\”
设置exl=CreateObject(“Excel.Application”)
用exl
设置wbk=.Workbooks.Open(路径和“bin\Integrated up with Summary.xlsm”)
'
设置sht=wbk.工作表(“摘要”)
用短小排序
.SortFields.Clear
.SortFields.Add键:=短范围(“J3:J11”)_
,SortOn:=xlSortOnValues,顺序:=xlDescending,数据选项:=xlSortNormal
.SetRange短量程(“C2:P11”)
.Header=xlYes
.MatchCase=False
.方向=xlTopToBottom
.SortMethod=xl拼音
.申请
以
'
wbk,结束
设置sht=无
设置wbk=Nothing
以
退出
端接头
将范围(“J3:J11”)
更改为范围(“J3:J11”)
,然后重试。类似于范围(“C2:P11”)
:)