VBA中打开多个文件有问题,请高手释疑
要打开2个文件,并读数。第一个文件打开了,数据也读出来了。但是执行到打开第二个文件时,报错“类型不匹配”。程序如下:(说这句有问题:Workbooks.Open filename(n),监视了,n=1可以,但是n=2的时候就报错了,高手释疑)
Option Explicit
Public filename
Public MySheet As Object
Public baogaosheet As Object
Public tmp_string As String
Public Sub CommandButton1_Click()
Dim n As Integer
filename = Application.GetOpenFilename(FileFilter:="Excel Files (*.xls), *.xls", Title:="打开生成的高速数据文件", MultiSelect:=True)
If IsArray(filename) Then
tmp_string = filename(1)
For n = 2 To UBound(filename)
tmp_string = tmp_string & ";" & filename(n)
Next
TextBox1.Text = tmp_string
Else
Exit Sub
End If
End Sub
Public Sub CommandButton2_Click()
Dim n As Integer
'Dim mysheetname As String
For n = 1 To UBound(filename)
Workbooks.Open filename(n)
Dim row As Long
Dim col As Long
Unload Me
Set baogaosheet = ThisWorkbook.Sheets("高速市区")
Set MySheet = ActiveWorkbook.Application.ActiveSheet
Application.StatusBar = "删除高速数据 "
DoEvents
For row = 3 To 17
For col = 2 To 8
baogaosheet.Cells(row, col).ClearContents
Next
Next
'高速数据
Application.StatusBar = "读取高速数据 "
DoEvents
Highwaybaogao.testdistance = MySheet.Cells(13, 3) - MySheet.Cells(15, 3) - MySheet.Cells(16, 3) - MySheet.Cells(17, 3) - MySheet.Cells(18, 3) - MySheet.Cells(19, 3) - MySheet.Cells(20, 3) - MySheet.Cells(21, 3) - MySheet.Cells(22, 3) - MySheet.Cells(23, 3) - MySheet.Cells(24, 3) - MySheet.Cells(25, 3) - MySheet.Cells(26, 3)
Highwaybaogao.distancecover = MySheet.Cells(35, 7)
Highwaybaogao.connectrate = MySheet.Cells(59, 8)
Highwaybaogao.dropcount = MySheet.Cells(59, 6) + MySheet.Cells(59, 7)
Highwaybaogao.mosavg = MySheet.Cells(44, 11)
Application.StatusBar = "填写高速数据 "
DoEvents
baogaosheet.Cells(3, 2) = Highwaybaogao.testdistance
baogaosheet.Cells(3, 3) = Highwaybaogao.distancecover
baogaosheet.Cells(3, 4) = Highwaybaogao.connectrate
baogaosheet.Cells(3, 5) = Highwaybaogao.dropcount
baogaosheet.Cells(3, 7) = Highwaybaogao.mosavg
If Highwaybaogao.dropcount = 0 Then
baogaosheet.Cells(3, 6) = "∞"
Else
baogaosheet.Cells(3, 6) = Highwaybaogao.testdistance / Highwaybaogao.dropcount
End If
Next
ActiveWorkbook.Close
Application.StatusBar = "高速数据填写完成 "
DoEvents
End Sub