Option Explicit Dim ex As Excel.Application Dim wb As Excel.Workbook Dim sh As Excel.WorksheetPrivate Sub Form_Load() On Error GoTo err1 Set ex = New Excel.Application Set wb = ex.Workbooks.Open("C:\Documents and Settings\yong.ZZY\桌面\book1.xls") Set sh = wb.Sheets(1) Debug.Print sh.Name Dim arr(1, 1) Dim i As Integer, j As Integer For i = 1 To 2 For j = 1 To 2 arr(i - 1, j - 1) = sh.Cells(i, j).Value Debug.Print arr(i - 1, j - 1) Next j Next iExit Sub err1:Set sh = Nothing Set wb = Nothing Set ex = Nothing Debug.Print Err.Description End SubPrivate Sub Form_Unload(Cancel As Integer)Set sh = Nothing Set wb = Nothing Set ex = NothingEnd Sub
忘了关闭了 wb.Close ex.Quit
Dim strFileName As String strFileName = GetExcelFileName("客户资料表", dlg)
Dim objApp As New Excel.Application Dim exBook As Excel.Workbook Dim exSheet As Excel.Worksheet
Set exBook = objApp.Workbooks.Open(strFileName) Set exSheet = exBook.Worksheets(1) //存放在varCustomerList中 Dim varCustomerList As Variant
Option Explicit
Dim ex As Excel.Application
Dim wb As Excel.Workbook
Dim sh As Excel.WorksheetPrivate Sub Form_Load()
On Error GoTo err1
Set ex = New Excel.Application
Set wb = ex.Workbooks.Open("C:\Documents and Settings\yong.ZZY\桌面\book1.xls")
Set sh = wb.Sheets(1)
Debug.Print sh.Name
Dim arr(1, 1)
Dim i As Integer, j As Integer
For i = 1 To 2
For j = 1 To 2
arr(i - 1, j - 1) = sh.Cells(i, j).Value
Debug.Print arr(i - 1, j - 1)
Next j
Next iExit Sub
err1:Set sh = Nothing
Set wb = Nothing
Set ex = Nothing
Debug.Print Err.Description
End SubPrivate Sub Form_Unload(Cancel As Integer)Set sh = Nothing
Set wb = Nothing
Set ex = NothingEnd Sub
wb.Close
ex.Quit
Dim strFileName As String
strFileName = GetExcelFileName("客户资料表", dlg)
Dim objApp As New Excel.Application
Dim exBook As Excel.Workbook
Dim exSheet As Excel.Worksheet
Set exBook = objApp.Workbooks.Open(strFileName)
Set exSheet = exBook.Worksheets(1)
//存放在varCustomerList中
Dim varCustomerList As Variant
varCustomerList = exSheet.UsedRange.Value