Visio中通过VBA宏排序Excel OLE对象报错问题排查
Visio中操作Excel OLE对象排序触发Error 1004问题
我正在用VBA对Visio中的Excel OLE对象内的数据进行排序,排序前的语句均可正常执行(已确认能正确选中目标区域),但执行xlSheet.Range.Sort语句时,触发「Error 1004: Sort method of Range class failed」错误。
已尝试以下操作但未解决问题:
- 咨询Copilot
- 查阅Stack Overflow(确认Ascending拼写正确)
- 从Visio中打开该Excel对象,在Excel内添加排序宏可正常运行
- 仅对B列(B2:B17)排序,仍出现相同错误
- 尝试包含表头,无效果
我的代码如下:
Dim xlApp As Object Dim xlSheet As Object Dim visPage As Visio.Page Dim CList As Object ' 获取当前选中的Visio页面 Set visPage = Visio.ActiveWindow.Page ' 从Visio页面获取Excel对象 Set CList = ActivePage.OLEObjects("Cable List").Object ' 获取Excel应用和工作表 Set xlApp = CList.Application Set xlSheet = CList.Sheets(1) ' 按B列排序B2:E17区域 xlSheet.Range("B2:E17").Select xlSheet.Range("B2:E17").Sort Key1:=xlSheet.Range("B2"), Order1:=xlAscending, Header:=xlNo ' 刷新Visio页面 visPage.Refresh ' 清理对象 Set xlSheet = Nothing Set xlApp = Nothing Set CList = Nothing
内容的提问来源于stack exchange,提问作者user2705985
相关产品推荐
相关产品推荐

