Sub OpenE()
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Dim strname as String
strname=Inputbox "请输入文件名:"
Set xlBook = xlApp.Workbooks.Open(strname)
xlApp.Application.Visible = True
xlBook.Application.Sheets(1).Select
End Sub
--------------------------------------------------------
如果使用窗体中某控件的变量,则可以
Sub OpenE(strname)
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Set xlBook = xlApp.Workbooks.Open(strname)
xlApp.Application.Visible = True
xlBook.Application.Sheets(1).Select
End Sub
调用:
call openE(me.文件名.value)
------------------------------------------------------
如文件需要查找后打开,则:
Sub OpenE(strname)
Dim xlApp As New Excel.Application
Dim xlBook As Excel.Workbook
Set xlBook = xlApp.Workbooks.Open(strname)
xlApp.Application.Visible = True
xlBook.Application.Sheets(1).Select
End Sub
Function GetFile() As String
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
GetFile = dlgOpen.SelectedItems(1)
Set dlgOpen = Nothing
End Function