nix*_*xda 3 powerpoint vba excel-2003
问:如何从 Excel 工作表中获取特定行中最后使用的列。
虽然使用 Excel VBA 这是一项简单的任务,但使用 Powerpoint VBA 则更加困难。
这是我的 Excel 工作表的示例
重现步骤
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
Run Code Online (Sandbox Code Playgroud)
期望的结果:我想要得到lastcol = 3
我的代码中的注释向您展示了我到目前为止所尝试的内容以及它们产生的错误。
我很感谢任何建议。
没有 PowerPoint 2003 标签?哦
您发布的 Excel 特定代码应该能够实际工作,并给出与在 Excel 中执行它相同的结果。
您收到错误消息的原因很可能是您没有在 Powerpoint VBA 中包含 Excel 引用!您确实使用了后期绑定,因此通常不需要引用该库。但是,VBA 编译器不知道 Excel 常量/枚举的内部值xlToLeft
,xlLastCell
因此xlPrevious
会产生错误!
解决您的问题的两种选择:
? xlToLeft
-,您将得到结果(在本例中-4159
)。然后用数字替换 Powerpoint VBA 中的名称,它应该可以工作。(为了更好的代码可读性,我通常将常量的名称留在同一行的注释中,例如lastcol = objWorkbook.Sheets(1).Cells.SpecialCells(11).Column 'xlLastCell = 11
归档时间: |
|
查看次数: |
3782 次 |
最近记录: |