我有这个问题,你能否在ex中返回一个excel的实例/对象? 我的意思是,每次我需要打开一个excel文档时,不是一遍又一遍地编写相同的代码。我想调用一个函数,打开excel这样的东西
Public Function TestExcel(ByVal filePath As String) As Object
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(filePath)
Set oSheet = oBook.Sheets(1)
Set TestExcel = oSheet
End Function
Sub test()
Dim oSheet As Object
Set oSheet = TestExcel("C:\Users\MYfile")
test = oSheet.Range("B" & excelRow).Value
'Or something like this
number = 10
osheet.Range("B" & excelRow).Value = number
End Sub
答案 0 :(得分:0)
function functionname(args) as excel.workbook
并且在此功能中,通常最后,您使用帖子说set functionname = oBook
例如
Function OpenWorkBook(strPath as string) as Excel.workbook
set OpenWorkbook=workbooks.open (strPath)
end function
及其使用
dim x as excel.workbook
set x=openworkbook("c:\test.xlsx")