【发布时间】:2019-11-20 05:32:24
【问题描述】:
问:如何从 Excel 工作表中获取特定行中最后使用的列。
虽然这是a simple task using Excel VBA,但使用 Powerpoint VBA 更加困难。
这是我的 Excel 工作表的示例
复制步骤
- 创建并保存一个新的 Excel 工作表,如我上面的示例
- 打开一个新的 PowerPoint 演示文稿并按 ALT+F11 以使用 VBA 编辑器
- 插入下面的代码并在第 3 行自定义测试 Excel 文件的路径
- 使用 F8 执行每行代码
Sub findlastcolumn()
Set objExcel = CreateObject("Excel.application")
Set objWorkbook = objExcel.Workbooks.Open("C:\Users\<USERNAME>\Desktop\data.xls")
objExcel.Visible = True
'works in Powerpoint VBA but delivers the wrong column
lastcol = objWorkbook.Sheets(1).UsedRange.Columns.Count
'produces an error in Powerpoint VBA
'but works in Excel VBA, correct column in Excel VBA
lastcol = objWorkbook.Sheets(1).Cells(1, 254).End(Direction:=xlToLeft).Column
'produces an error in Powerpoint VBA
'but works in Excel VBA, and wrong column in Excel VBA too
lastcol = objWorkbook.Sheets(1).Cells.SpecialCells(xlLastCell).Column
'wrong column. Powerpoint VBA' find method differs from Excel' find method
'searchdirection isn't available in Powerpoint VBA
lastcol = objWorkbook.Sheets(1).Rows(1).Find(what:="*", _
after:=objWorkbook.Sheets(1).Cells(1, 1), searchdirection:=xlPrevious).Column
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
End Sub
想要的结果:我想得到 lastcol = 3
我的代码中的 cmets 向您展示了我迄今为止所做的尝试以及它们产生的错误。
我很感谢您的任何建议。
没有 PowerPoint 2003 标签? O.o
【问题讨论】:
-
+1 表示“问得很漂亮的问题”。建议您编辑示例 - 将代码稍微包装一下以提高可读性...
标签: vba powerpoint excel-2003