用vb编写的一个小程序,输出数据保存为excel表,在中文系统和这边安装的英文操作系统中编译运行都没问题,但拿到国外(德国和澳洲)去安装执行保存数据这一步时就出现这样的错误:RUN-Time error '9' Subscript out of range
然后拿到对方给的操作系统安装盘,模拟了对方的运行环境也不会出现这个问题!!!
现在最大的问题是我这里一直测试不出这个错误,不知道怎么调试程序(已经请人帮忙修改了,但还是没解决问题)
请问是什么原因啊?有什么办法可以解决啊?(很有挑战性啊!!!!)
thx
然后拿到对方给的操作系统安装盘,模拟了对方的运行环境也不会出现这个问题!!!
现在最大的问题是我这里一直测试不出这个错误,不知道怎么调试程序(已经请人帮忙修改了,但还是没解决问题)
请问是什么原因啊?有什么办法可以解决啊?(很有挑战性啊!!!!)
thx
只能在定义的范围内访问数组元素和集合成员。此错误有以下的原因和解决方法: 引用了不存在的数组元素.
下标比可能下标范围大或小,或是在应用程序中这一边的数组没有指定范围。检查数组的声明以确认其上界和下界。若使用的是重新指定范围的数组,应使用 UBound 和 LBound 函数来决定数组访问。如果索引指定为变量,应检查变量名的拼写。声明数组时没有指定元素的数目。例如,下列的代码就会导致此错误:
Dim MyArray() As Integer
MyArray(8) = 234 ' 导致错误 9。Visual Basic 并不会将没有指定范围的数组自动设为 0 – 10。相反必须使用 Dim 或 ReDim 来指定数组中元素的数目。引用了不存在的集合成员。
试着使用 For Each...Next 结构代替指定元素下标。使用速写形式的下标,结果指定了错误的元素。
例如,当在集合上使用 ! 运算子时,! 自动指定了一个键。例如 object!keyname.value 和 object.item(keyname).value 是一样的。在此例中,集合中如果 keyname 表示一个错误键,错误就会产生。若要改进此错误,在集合对象中使用正确的键名称或索引。
看这个KB:http://support.microsoft.com/default.aspx?scid=kb;en-us;293921
数组重新指定范围时也是用 UBound的
Dim cnt As New ADODB.Connection
Dim rst As New ADODB.Recordset Dim xlApp As Object
Dim xlWb As Object
Dim xlWs As Object
Dim recArray As Variant Dim strDB As String
Dim fldCount As Integer
Dim recCount As Long
Dim iCol As Integer
Dim iRow As Integer
'-------------保存为access-----------------------------------------------------------------------------------
If Option1.Value = True Then
Filem = Trim(Text1.Text)
If Filem = "" Then
Text1.SetFocus
Exit Sub
End If
'MsgBox Dir1.Path
'---------------------------保存
If Right(Dir1.Path, 1) = "\" Then
pafm = Dir1.Path + Filem
Else
pafm = Dir1.Path + "\" + Filem
End If
If Dir(pafm) = "" Then
'-----------------可以保存
' FileCopy (App.Path + "\db1.mdb"), (pafm + ".mdb")
‘-----------再将其转换成excel-----------------
strDB = App.Path & "\db1.mdb"
cnt.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strDB & ";"
'rst.Open "select NO,FUN,DATA,UNIT,DATA FROM b1", cnt
rst.Open "select * from b1", cnt
Set xlApp = CreateObject("Excel.Application")
Set xlWb = xlApp.Workbooks.Add
Set xlWs = xlWb.Worksheets("Sheet1") xlApp.Visible = False
fldCount = rst.Fields.Count
For iCol = 1 To fldCount
xlWs.Cells(1, iCol).Value = rst.Fields(iCol - 1).Name
Next If Val(Mid(xlApp.Version, 1, InStr(1, xlApp.Version, ".") - 1)) > 8 Then
xlWs.Cells(2, 1).CopyFromRecordset rst
Else
recArray = rst.GetRows recCount = UBound(recArray, 2) + 1
'这里是用ubound来决定数组的啊
For iCol = 0 To fldCount - 1
For iRow = 0 To recCount - 1
If IsDate(recArray(iCol, iRow)) Then
recArray(iCol, iRow) = Format(recArray(iCol, iRow))
ElseIf IsArray(recArray(iCol, iRow)) Then
recArray(iCol, iRow) = "Array Field"
End If
Next iRow
Next iCol
xlWs.Cells(2, 1).Resize(recCount, fldCount).Value = _
TransposeDim(recArray)
End If xlApp.Selection.CurrentRegion.Columns.AutoFit
xlApp.Selection.CurrentRegion.Rows.AutoFit
' Close ADO objects
rst.Close
cnt.Close
Set rst = Nothing
Set cnt = Nothing
xlWs.SaveAs pafm + ".xls"
xlApp.Quit
MsgBox "Save successfully!", vbOKCancel, "information"
' Release Excel references
Set xlWs = Nothing
Set xlWb = Nothing Set xlApp = Nothing
但是拿到国外去安装运行时就出现RUN-TIME ERROR 9的错误了
现在关键问题是我测试不出到底错在哪
就不知道该如何调试修改了:(