用VB调用EXCEL排序的问题,(实时错误 '1004' 对象 'Range' 的方法 '_Global' 失败)
排序的代码如下:
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("C2"), Order2:=xlDescending, Key3:=Range("D2"), Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal第一次运行时一切正常,但按CTRL+ALT+DEL,仍可在关闭程序窗口看见EXCEL,
第二次运行时出现:实时错误 '1004' 对象 'Range' 的方法 '_Global' 失败退出EXCEL的语句如下:
myBook.Close
myExcel.Quit
Set mySheet = Nothing
Set myBook = Nothing
Set myExcel = Nothing是我的语句有问题,还是需要对排序的代码进行修改?谢谢。
只有25分了。不够,请多支持。谢谢。
排序的代码如下:
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("C2"), Order2:=xlDescending, Key3:=Range("D2"), Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal第一次运行时一切正常,但按CTRL+ALT+DEL,仍可在关闭程序窗口看见EXCEL,
第二次运行时出现:实时错误 '1004' 对象 'Range' 的方法 '_Global' 失败退出EXCEL的语句如下:
myBook.Close
myExcel.Quit
Set mySheet = Nothing
Set myBook = Nothing
Set myExcel = Nothing是我的语句有问题,还是需要对排序的代码进行修改?谢谢。
只有25分了。不够,请多支持。谢谢。
把new去掉试试 也许进程里的excel会随着Nothing消失
'*********************杀死Excel进程*************************
On Error Resume Next
Dim s
s = "excel.exe"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name='" & s & "'")
For Each objProcess In colProcessList
objProcess.Terminate
Next
'*************************************************************
贴多点代码出来.. 创建EXCEL部分 出错部分 关闭EXCEL 部分
这样没用。会不会是排序的时候,出的问题?我这个语句是从宏里COPY出来的。
Dim myExcel As Excel.Application '声明对象变量
Dim myBook As Excel.Workbook
Dim mySheet As Excel.Worksheet
Set myExcel = Excel.Application '初始化对象变量
myExcel.SheetsInNewWorkbook = 1 ''将新建的工作薄数量设为1
Set myBook = myExcel.Workbooks.Add '增加一个工作薄
myBook.Sheets(myBook.Sheets.Count).Name = "直选单式" '修改工作薄名称
排序
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("C2"), Order2:=xlDescending, Key3:=Range("D2"), Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal退出语句
myBook.SaveAs ("号码分析") '文件另存为
myExcel.Application.IgnoreRemoteRequests = False
myBook.Close
myExcel.Quit
Set mySheet = Nothing
Set myBook = Nothing
Set myExcel = Nothing
Me.MousePointer = 0
Set mySheet = Nothing
myBook.SaveAs ("号码分析") '文件另存为
myBook.Close
Set myBook = Nothing
myExcel.Application.IgnoreRemoteRequests = False
myExcel.Quit
Set myExcel = Nothing
Selection.Sort ...
上面的方式只能在 Excel 的宏(VBA)中使用,因为 Selection 就是当前 Excel 实例的全局对象。
但是在 Excel 外( VB 中)调用时,往往与宏中的指向不一致,要直接用具体对象进行指定
mySheet.Range("A1:A5").Sort ...
Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("C2"), Order2:=xlDescending, Key3:=Range("D2"), Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal要怎么改?我改为:Selection.Sort Key1:=Range("B2:B20000"), Order1:=xlDescending, Key2:=Range("C2:C20000"), Order2:=xlDescending, Key3:=Range("D2:D20000"), Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal
还是不行。
谢谢您。
Range("B2") 的形式不能用,要将前缀限定为 mySheet.Range("B2")
我改成了上面这样,也是不行。
为什么第一次运行都没有问题,第二次就运行不行。仍可在关闭程序窗口看见EXCEL,
不能直接用的,第一次会成功,但之后就会出错,应该修改成具体的单元框
Dim myExcel As Excel.Application '声明对象变量
Dim myBook As Excel.Workbook
Dim mySheet As Excel.Worksheet
Set myExcel = Excel.Application '初始化对象变量
myExcel.SheetsInNewWorkbook = 1 ''将新建的工作薄数量设为1
Set myBook = myExcel.Workbooks.Add '增加一个工作薄
myBook.Sheets(myBook.Sheets.Count).Name = "直选单式" '修改工作薄名称排序myExcel.Selection.Sort Key1:=Range("B2"), Order1:=xlDescending, Key2:=Range("C2"), Order2:=xlDescending, Key3:=Range("D2"), Order3:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, DataOption3:=xlSortNormal退出语句
myBook.SaveAs ("号码分析") '文件另存为
myExcel.Application.IgnoreRemoteRequests = False
myBook.Close
myExcel.Quit
Set mySheet = Nothing
Set myBook = Nothing
Set myExcel = Nothing
Me.MousePointer = 0你试一下
因为单独的 Excel 关闭时,这些全局对象会释放。
而你通过 VB 程序调用,程序不关闭这些全局对象就不会释放,导致 Excel 进程残留。你第一次调用的这些全局对象就是当前 Excel 的对象,正确。
而第二次调用的这些全局对象还是上次 Excel 的对象,错误。
可以帮我改下吗?
With xlSheet.Range(xlSheet.Cells(8, 1), xlSheet.Cells(k + 6, 16)).Sort Range("N8")