Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 7205|回复: 8

[已解决]对象关闭时,不允许操作,运行时错误3704

[复制链接]
发表于 2013-11-7 23:23 | 显示全部楼层 |阅读模式
本帖最后由 张雄友 于 2013-11-8 18:43 编辑

对象关闭时,不允许操作,运行时错误3704
最佳答案
2013-11-8 08:20
03的格式也能导入呀。
  1. Sub 合并数据()
  2.     Dim cnn As Object, rs As Object
  3.     Dim SQL$, MyFile$, s$, m&, n&, t$, i%, z$, v%
  4.    
  5.     With Application.FileDialog(msoFileDialogFolderPicker)
  6.         .InitialFileName = ThisWorkbook.Path & ""
  7.         If .Show = False Then Exit Sub
  8.         MyPath = .SelectedItems(1) & ""
  9.     End With

  10.     Dim msg As String, nsg As String, BBB As String
  11.     Application.ScreenUpdating = False
  12.     Set cnn = CreateObject("ADODB.Connection")
  13.     Set cat = CreateObject("ADOX.Catalog")
  14.     Cells.ClearContents
  15.     MyFile = Dir(MyPath & "*.xls")
  16.     On Error Resume Next
  17.     Do While MyFile <> ""
  18.         If MyFile <> ThisWorkbook.Name Then
  19.             n = n + 1
  20.             If n = 1 Then
  21.                 cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0';"
  22.             Else
  23.                 t = "[Excel 12.0;Database=" & MyPath & MyFile & "]."
  24.             End If
  25.             cat.ActiveConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0';"
  26.             For Each tb1 In cat.Tables
  27.                 If tb1.Type = "TABLE" Then
  28.                     z = ""
  29.                     z = tb1.Columns(0).Name
  30.                     If Left$(z, 1) <> "F" Then
  31.                         s = Replace(tb1.Name, "'", "")
  32.                         If Right(s, 1) = "$" Then
  33.                             v = v + 1
  34.                             If v = 1 Then
  35.                                 Set rs = cnn.Execute("[" & s & "]")
  36.                                 For i = 1 To rs.Fields.Count
  37.                                     Cells(1, i) = rs.Fields(i - 1).Name
  38.                                 Next
  39.                             End If
  40.                             m = m + 1
  41.                             If m > 49 Then
  42.                                 Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  43.                                 m = 1
  44.                                 SQL = ""
  45.                             End If
  46.                              If Len(SQL) Then SQL = SQL & " union all "

  47.                             SQL = SQL & "select * from " & t & "[" & s & "] "
  48.                         End If
  49.                     End If
  50.                 End If
  51.             Next
  52.         End If
  53.         MyFile = Dir()
  54.     Loop
  55.     On Error GoTo 0
  56.     If Len(SQL) Then Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  57.     cnn.Close
  58.     Set cnn = Nothing
  59.     Set cat = Nothing
  60.     Set tb1 = Nothing
  61.     Application.ScreenUpdating = True
  62. End Sub
复制代码

合并.rar

179.19 KB, 下载次数: 50

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
 楼主| 发表于 2013-11-8 00:18 | 显示全部楼层
附件是EXCEL 2010 格式文件,如果换成:

cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Extended Properties=Excel 12.0;Data Source=" & ThisWorkbook.FullName

又会提示:未找到提供程序,该程序可以未正确安装。


回复

使用道具 举报

发表于 2013-11-8 00:43 | 显示全部楼层
http://www.connectionstrings.com/excel/
连接字符串就不对。
activeconnection要的是活动连接,而不是连接字符串。
回复

使用道具 举报

 楼主| 发表于 2013-11-8 05:57 | 显示全部楼层
hwc2ycy 发表于 2013-11-8 00:43
http://www.connectionstrings.com/excel/
连接字符串就不对。
activeconnection要的是活动连接,而不是连 ...

怎么改才对?
回复

使用道具 举报

 楼主| 发表于 2013-11-8 06:52 | 显示全部楼层
如果明细中的EXCEL 是03版本就不会出错。
回复

使用道具 举报

 楼主| 发表于 2013-11-8 07:49 | 显示全部楼层
hwc2ycy 发表于 2013-11-8 00:43
http://www.connectionstrings.com/excel/
连接字符串就不对。
activeconnection要的是活动连接,而不是连 ...

难道是03版本的代码在10中就不适用了?
回复

使用道具 举报

发表于 2013-11-8 07:59 | 显示全部楼层
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\myFolder\myExcel2007file.xlsx;
Extended Properties="Excel 12.0 Xml;HDR=YES";
  1. Sub 合并数据()
  2.     Dim cnn As Object, rs As Object
  3.     Dim SQL$, MyFile$, s$, m&, n&, t$, i%, z$, v%
  4.     With Application.FileDialog(msoFileDialogFolderPicker)
  5.         .InitialFileName = ThisWorkbook.Path & ""
  6.         If .Show = False Then Exit Sub
  7.         MyPath = .SelectedItems(1) & ""
  8.     End With
  9.     Dim msg As String, nsg As String, BBB As String
  10.     Application.ScreenUpdating = False
  11.     Set cnn = CreateObject("ADODB.Connection")
  12.     Set cat = CreateObject("ADOX.Catalog")
  13.     Cells.ClearContents
  14.     MyFile = Dir(MyPath & "*.xlsx")
  15.     On Error Resume Next
  16.     Do While MyFile <> ""
  17.         If MyFile <> ThisWorkbook.Name Then
  18.             n = n + 1
  19.             If n = 1 Then
  20.                 'cnn.Open "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
  21.                 cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0 Xml';"

  22.             Else
  23.                 t = "[Excel 12.0;Database=" & MyPath & MyFile & "]."
  24.             End If
  25.             cat.ActiveConnection = cnn    '"Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
  26.             For Each tb1 In cat.Tables
  27.                 If tb1.Type = "TABLE" Then
  28.                     z = ""
  29.                     z = tb1.Columns(0).Name
  30.                     If Left$(z, 1) <> "F" Then
  31.                         s = Replace(tb1.Name, "'", "")
  32.                         If Right(s, 1) = "$" Then
  33.                             v = v + 1
  34.                             If v = 1 Then
  35.                                 Set rs = cnn.Execute("[" & s & "]")
  36.                                 For i = 1 To rs.Fields.Count
  37.                                     Cells(1, i) = rs.Fields(i - 1).Name
  38.                                 Next
  39.                             End If
  40.                             m = m + 1
  41.                             If m > 49 Then
  42.                                 Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  43.                                 m = 1
  44.                                 SQL = ""
  45.                             End If
  46.                             If Len(SQL) Then SQL = SQL & " union all "

  47.                             SQL = SQL & "select * from " & t & "[" & s & "] "
  48.                         End If
  49.                     End If
  50.                 End If
  51.             Next
  52.         End If
  53.         MyFile = Dir()
  54.     Loop
  55.     On Error GoTo 0
  56.     If Len(SQL) Then Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  57.     cnn.Close
  58.     Set cnn = Nothing
  59.     Set cat = Nothing
  60.     Set tb1 = Nothing
  61.     Application.ScreenUpdating = True
  62. End Sub
复制代码
回复

使用道具 举报

发表于 2013-11-8 08:10 | 显示全部楼层
后面的cat.activeconnection是可以用连接串的,我搞错了。
  1. Sub 合并数据()
  2.     Dim cnn As Object, rs As Object
  3.     Dim SQL$, MyFile$, s$, m&, n&, t$, i%, z$, v%
  4.     With Application.FileDialog(msoFileDialogFolderPicker)
  5.         .InitialFileName = ThisWorkbook.Path & ""
  6.         If .Show = False Then Exit Sub
  7.         MyPath = .SelectedItems(1) & ""
  8.     End With
  9.     Dim msg As String, nsg As String, BBB As String
  10.     Application.ScreenUpdating = False
  11.     Set cnn = CreateObject("ADODB.Connection")
  12.     Set cat = CreateObject("ADOX.Catalog")
  13.     Cells.ClearContents
  14.     MyFile = Dir(MyPath & "*.xlsx")
  15.     On Error Resume Next
  16.     Do While MyFile <> ""
  17.         If MyFile <> ThisWorkbook.Name Then
  18.             n = n + 1
  19.             If n = 1 Then
  20.                 'cnn.Open "Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
  21.                 cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0 Xml';"

  22.             Else
  23.                 t = "[Excel 12.0;Database=" & MyPath & MyFile & "]."
  24.             End If
  25.             cat.ActiveConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0 Xml';" '"Provider=Microsoft.jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & MyPath & MyFile
  26.             For Each tb1 In cat.Tables
  27.                 If tb1.Type = "TABLE" Then
  28.                     z = ""
  29.                     z = tb1.Columns(0).Name
  30.                     If Left$(z, 1) <> "F" Then
  31.                         s = Replace(tb1.Name, "'", "")
  32.                         If Right(s, 1) = "$" Then
  33.                             v = v + 1
  34.                             If v = 1 Then
  35.                                 Set rs = cnn.Execute("[" & s & "]")
  36.                                 For i = 1 To rs.Fields.Count
  37.                                     Cells(1, i) = rs.Fields(i - 1).Name
  38.                                 Next
  39.                             End If
  40.                             m = m + 1
  41.                             If m > 49 Then
  42.                                 Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  43.                                 m = 1
  44.                                 SQL = ""
  45.                             End If
  46.                              If Len(SQL) Then SQL = SQL & " union all "

  47.                             SQL = SQL & "select * from " & t & "[" & s & "] "
  48.                         End If
  49.                     End If
  50.                 End If
  51.             Next
  52.         End If
  53.         MyFile = Dir()
  54.     Loop
  55.     On Error GoTo 0
  56.     If Len(SQL) Then Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  57.     cnn.Close
  58.     Set cnn = Nothing
  59.     Set cat = Nothing
  60.     Set tb1 = Nothing
  61.     Application.ScreenUpdating = True
  62. End Sub
复制代码
回复

使用道具 举报

发表于 2013-11-8 08:20 | 显示全部楼层    本楼为最佳答案   
03的格式也能导入呀。
  1. Sub 合并数据()
  2.     Dim cnn As Object, rs As Object
  3.     Dim SQL$, MyFile$, s$, m&, n&, t$, i%, z$, v%
  4.    
  5.     With Application.FileDialog(msoFileDialogFolderPicker)
  6.         .InitialFileName = ThisWorkbook.Path & ""
  7.         If .Show = False Then Exit Sub
  8.         MyPath = .SelectedItems(1) & ""
  9.     End With

  10.     Dim msg As String, nsg As String, BBB As String
  11.     Application.ScreenUpdating = False
  12.     Set cnn = CreateObject("ADODB.Connection")
  13.     Set cat = CreateObject("ADOX.Catalog")
  14.     Cells.ClearContents
  15.     MyFile = Dir(MyPath & "*.xls")
  16.     On Error Resume Next
  17.     Do While MyFile <> ""
  18.         If MyFile <> ThisWorkbook.Name Then
  19.             n = n + 1
  20.             If n = 1 Then
  21.                 cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0';"
  22.             Else
  23.                 t = "[Excel 12.0;Database=" & MyPath & MyFile & "]."
  24.             End If
  25.             cat.ActiveConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & MyPath & MyFile & "';Extended Properties='Excel 12.0';"
  26.             For Each tb1 In cat.Tables
  27.                 If tb1.Type = "TABLE" Then
  28.                     z = ""
  29.                     z = tb1.Columns(0).Name
  30.                     If Left$(z, 1) <> "F" Then
  31.                         s = Replace(tb1.Name, "'", "")
  32.                         If Right(s, 1) = "$" Then
  33.                             v = v + 1
  34.                             If v = 1 Then
  35.                                 Set rs = cnn.Execute("[" & s & "]")
  36.                                 For i = 1 To rs.Fields.Count
  37.                                     Cells(1, i) = rs.Fields(i - 1).Name
  38.                                 Next
  39.                             End If
  40.                             m = m + 1
  41.                             If m > 49 Then
  42.                                 Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  43.                                 m = 1
  44.                                 SQL = ""
  45.                             End If
  46.                              If Len(SQL) Then SQL = SQL & " union all "

  47.                             SQL = SQL & "select * from " & t & "[" & s & "] "
  48.                         End If
  49.                     End If
  50.                 End If
  51.             Next
  52.         End If
  53.         MyFile = Dir()
  54.     Loop
  55.     On Error GoTo 0
  56.     If Len(SQL) Then Range("a" & Rows.Count).End(xlUp).Offset(1).CopyFromRecordset cnn.Execute(SQL)
  57.     cnn.Close
  58.     Set cnn = Nothing
  59.     Set cat = Nothing
  60.     Set tb1 = Nothing
  61.     Application.ScreenUpdating = True
  62. End Sub
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-20 02:17 , Processed in 0.401448 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表