0

我一直在尝试在后台有一个 excel 数据库的 powerpoint 上工作。现在我在通过表格作为 PPT VBA 的争论时遇到了麻烦。lastrow 和 lastcoulmn 函数返回“未定义用户定义类型”的错误。帮助将不胜感激。谢谢。

Dim oXLApp As Object
Dim oWb As Object
Dim Deps As Excel.Range
Dim Dep, Shift, Name, EmpNo, Sup As String
Dim Sups As Excel.Range
Dim Shifts As Excel.Range

Public Sub getexceldata()
Dim str As String
Set oXLApp = CreateObject("Excel.Application")
Set oWb = oXLApp.Workbooks.Open(ActivePresentation.Path & "\" & "Property Wide.xlsm")

'Shifts
Set Shifts = oWb.Sheets(4).Range("A1:A" & lastRow(oWb.Sheets(4), "a"))

'departments
Set Deps = oWb.Sheets(3).Range("A1:" & Chr(lastColumn(oWb.Sheets(3), "1") + 64) & "1")

'supervisors

End Sub


Public Function lastRow(ByVal SheetA As Excel.Application.Sheet, Optional Columnno As Char = "/")     As Long
If (Columnno = "/") Then
Set lastRow = SheetA.UsedRange.Row - 1 + SheetA.UsedRange.Rows.Count
Else
Set lastRow = SheetA.Range(Columno & Rows.Count).End(xlUp).Row
End If
End Function

Public Function lastColumn(ByVal SheetA As Excel.Application.Sheet, Optional rowno As Char = "/")     As Integer
If (rowno = "/") Then
Set lastColumn = SheetA.UsedRange.Column - 1 + SheetA.UsedRange.Columns.Count
Else
Set lastColumn = SheetA.Cells(rowno, Columns.Count).End(xlToLeft).Column
End If
End Function
4

1 回答 1

0

第一个问题是 CHAR 不是有效的变量类型,所以我建议将其更改为字符串。

接下来确保在您的代码参考中包含 Microsoft Office Excel 14.0 对象库。

有了它,您可以对您的代码进行一些细微的调整,一切都应该正常工作。

Dim oXLApp As Object
Dim oWb As Object
Dim Deps As Excel.Range
Dim Dep, Shift, Name, EmpNo, Sup As String
Dim Sups As Excel.Range
Dim Shifts As Excel.Range

Public Sub getexceldata()
Dim str As String
Set oXLApp = CreateObject("Excel.Application")
Set oWb = oXLApp.Workbooks.Open(ActivePresentation.Path & "\" & "Property Wide.xlsm")

'Shifts
Set Shifts = oWb.Sheets(4).Range("A1:A" & lastRow(oWb.Sheets(4), "a"))

'departments
Set Deps = oWb.Sheets(3).Range("A1:" & Chr(lastColumn(oWb.Sheets(3), "1") + 64) & "1")

'supervisors

End Sub


Public Function lastRow(ByVal SheetA As Worksheet, Optional Columnno As String = "/")     As Long
If (Columnno = "/") Then
Set lastRow = SheetA.UsedRange.Row - 1 + SheetA.UsedRange.Rows.Count
Else
Set lastRow = SheetA.Range(Columno & Rows.Count).End(xlUp).Row
End If
End Function

Public Function lastColumn(ByVal SheetA As Worksheet, Optional rowno As String = "/")     As Integer
If (rowno = "/") Then
Set lastColumn = SheetA.UsedRange.Column - 1 + SheetA.UsedRange.Columns.Count
Else
Set lastColumn = SheetA.Cells(rowno, Columns.Count).End(xlToLeft).Column
End If
End Function

有了它,你应该有你需要的东西。

于 2014-12-15T21:43:16.853 回答