【发布时间】:2015-08-06 22:45:23
【问题描述】:
我在 Access 中使用 VBA 宏将数据放入 Excel。到目前为止一切进展顺利,但现在我在 Excel 中有数据,我想根据我创建的这个新列对数据进行排序。我搜索了不同的排序语法示例,但没有任何效果。最接近的是这个,它给了我一个运行时错误 438“对象不支持这个属性或方法”。我要做的就是根据第一列的数据(只是数字)对整个工作表中的数据进行排序。
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.Sheets("Sheet1").Select
End With
'More code is here in between that gets data into Excel file
'This code not shown works as expected
With xlApp
.Range("A1").EntireColumn.Insert
.Cells(1, 1).Value = "DayOfWeek"
For i = 2 To 10000
.Cells(i, 1).Value = Weekday(xlApp.Cells(i, 2).Value, vbMonday)
Next i
'Works as expected up to here, next line is problem
.Sort Key1:=.Range("A2"), Order1:=xlAscending
End With
编辑:在我初始化 xlApp 的部分中添加,并修复了 Order1 并将 Value 添加到 Cells 调用以使代码更清晰,如建议的那样。另外,应该注意的是,在调试过程中,我可以看到直到排序调用的所有内容都在 Excel 工作簿中给出了正确的输出。
【问题讨论】:
-
您能否使用 F8 键跟踪您的过程并告诉错误发生在哪一行?
-
它出现在 .Sort 行。抱歉,我试图将其评论为问题行
-
Sort() 不是 Excel 应用程序对象的方法。也许试试
.Range("A1").CurrentRegion.Sort Key1:=.Range("A2"), Order:=xlAscending -
尝试出现运行时错误 1004“应用程序定义或对象定义错误”
-
你能展示更多你的代码吗?特别是
xlApp是如何声明和分配的?