vba-函数返回sheet
'打开文件返回Sheet
Public Function GetSheetByOpenFile() As Worksheet
ifilename = Application.GetOpenFilename("Excel(*.xlsx), *.xlsx, Excel(*.xls), *.xls", False)
If ifilename <> "False" Then
Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Set xlApp = New Excel.Application
Set xlBook = xlApp.Workbooks.Open(ifilename)
Dim sheet As Excel.Worksheet
Set sheet = xlBook.Sheets(1)
Set GetSheetByOpenFile = sheet
Else
MsgBox "Please select a file first!", vbOKOnly, "Reminder"
Exit Function
End If
On Error Resume Next
Set xlBook = Nothing
Set xlApp = Nothing
End Function