一个困扰多日的问题
请教各位老师我把代码封装到DLL中从操作表中取数据,可以取到,但是运行到最后却出错,麻烦老师们帮忙看看,见附件 [ 本帖最后由 wangjiangtin 于 2011-1-25 14:56 编辑 ]
Private Sub Command1_Click() On Error Resume Next Dim SQL As String Dim cnn As Object Dim ADOX As Object, TableName As String Dim I As Integer Dim falg As Integer Dim xz Set xlapp = GetObject(, "Excel.Application") If daorushuju.List1.ListIndex = -1 Then MsgBox "请选中要导出的报表!", vbInformation, "导出系统提示" Exit Sub End If With daorushuju.List1 For I = 0 To .ListCount - 1 '在条目中循环 If .Selected(I) Then xz = .List(I) '将选中的条目给变量sht '---------------------导入数据过程代码------------------------------------------------------------------------ Set cnn = CreateObject("adodb.connection") cnn.Open "provider=microsoft.jet.oledb.4.0;extended properties='excel 8.0;hdr=no;imex=1;';data source=" & MyFileName SQL = "select * from [" & xz & "$]" If InStr(xz, "$") > 0 Then SQL = "select * from [" & xz & "]" ' Sheets("wang").Range("a4").CopyFromRecordset cnn.Execute(SQL) With xlapp.ActiveWorkbook.ActiveSheet ' .Cells.Clear '清除全部 .Rows("4:65536").Delete '删除全部行 ' .Range("4:65536").ClearContents '清除全部功能同上 .Range("a4").CopyFromRecordset cnn.Execute(SQL) '写入数据 ' .UsedRange.Borders.LineStyle = xlContinuous '添加格线 X = .UsedRange.Columns.Count '使用区域的列数 xx = .UsedRange.Rows.Count '使用区域的行数 .Range(Cells(4, 1), Cells(xx + 3, X)).Borders.LineStyle = xlContinuous '添加格线==+3是因为从a4写入的数据--Cells(4, 1)=a4 .Range(Cells(4, 1), Cells(xx + 3, X)).Font.Size = 10 '第4行以下设置为10号字体 End With cnn.Close Set cnn = Nothing '------------------------------------------------------------------------------------------------------------- End If Next End With Set xlapp = Nothing '清除定义的对象为空 xlapp.ActiveWorkbook.ActiveSheet Unload Me End Sub