VB编程问题
Private Sub Command5_Click()
Dim i As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("c:\T.xls")
Set xlSheet = xlBook.Worksheets
For i = 1 To 2048
a(i - 1) = xlSheet(1).Cells(i + 368, 1)
Next
xlApp.DisplayAlerts = False
xlBook.Close
xlApp.Quit
For i = 1 To 2415
b(i - 1) = i - 1
Next i
End Sub
我想用CommonDialog控件控制第七行指令 Set xlBook = xlApp.Workbooks.Open("c:\T.xls") 中打开文件的路径,应该怎么编程?
答案:Option Explicit
Private Sub Command1_Click()
Dim s As String
With CreateObject("MSComDlg.CommonDialog")
.Filter = "Excel文件|*.xls|All Files|*.*"
.ShowOpen
s = .FileName
End With
If s = "" Then Exit Sub
Dim i As Long
Dim xlApp As Object
Dim xlBook As Object
Dim xlSheet As Object
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open(s) Set xlSheet = xlBook.Worksheets
For i = 1 To 2048
a(i - 1) = xlSheet(1).Cells(i + 368, 1)
Next
xlApp.DisplayAlerts = False
xlBook.Close
xlApp.Quit
For i = 1 To 2415
b(i - 1) = i - 1
Next i
End Sub
比如:
dim s as string
commendialog.showopen
s=commendialog.filename
Set xlBook = xlApp.Workbooks.Open s
这样可以把打开文件的路径以参数形式传递
上一个:VB编程问题。
下一个:VB编程题目。。。。