我正在尝试编写如下函数:
Function getEntityCode(fileName As String) As Range
Dim entitySheet As Worksheet
Dim c As Range
Dim i As Integer
Dim valid As Boolean
Dim finalRow As Integer
Dim finalCol As Integer
Set entitySheet = Workbooks("OHUploads.xlsm").Worksheets("EntityCodes")
finalRow = entitySheet.Cells(Application.Rows.Count, 1).End(xlUp).Row
On Error GoTo ErrHandler:
With entitySheet
Set c = .Range(.Cells(1, 3), .Cells(1, finalCol))
End With
valid = False
ErrHandler:
Debug.Print Error(Err)
If valid = False Then
Set getEntityCode = entitySheet.Range(entitySheet.Cells(1, 3), entitySheet.Cells(1, finalCol))
Else
Set getEntityCode = c
End If
End Function
但是不管怎么做,当语句执行到 Set c = .Range(.Cells(1, 3), .Cells(i, finalCol)) 时总是在errHandler中结束,而且总是显示:Application-defined或对象定义的错误
我以为我已经明确定义了所有内容,看不到我做错了什么,请任何人帮助我,非常感谢。