Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
楼主: 张雄友

[已解决]复制或提取数据

[复制链接]
 楼主| 发表于 2013-7-18 21:48 | 显示全部楼层
w2001pf 发表于 2013-7-18 21:45
你发的附件就只有一个的,而且你的要求也没有说清楚的。如果需要做再继续发问吧?

见附件。

多表提取数据.rar

41.14 KB, 下载次数: 5

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
回复

使用道具 举报

发表于 2013-7-18 21:52 | 显示全部楼层
回复

使用道具 举报

 楼主| 发表于 2013-7-18 21:53 | 显示全部楼层
hwc2ycy 发表于 2013-7-18 21:52
直接用SQL不更方便嘛。

用SQL导入数据,单表的可以,多表的不能吧?据我所知。
回复

使用道具 举报

发表于 2013-7-18 22:08 | 显示全部楼层
  1. Const adUseClient = 3
  2. Const adModeShareDenyWrite = 8
  3. Const adModeReadWrite = 3
  4. Const adModeRead = 1
  5. Dim AdoConn As Object, AdoRst As Object

  6. Sub 提取数据()
  7.     Dim sht As Worksheet
  8.     Dim strSql As String
  9.     Dim strCode As String
  10.     strCode = Application.InputBox("请输入要查找的代码字段值,注意大小写", Type:=2)

  11.     If strCode = "False" Then
  12.         MsgBox "没有输入字段或没有确定,结束"
  13.         Exit Sub
  14.     End If

  15.     For Each sht In Worksheets
  16.         If sht.Name <> "提取数据" Then
  17.             strSql = strSql & "select * from [" & sht.Name & "$] where 代码='" & strCode & "' union all "
  18.         End If
  19.     Next

  20.     If Len(strSql) Then
  21.         strSql = Left(strSql, Len(strSql) - 11)
  22.         If ADOQuery(ThisWorkbook.FullName, strSql) Then
  23.             With Worksheets("提取数据")
  24.                 Dim lLastRow&
  25.                 lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
  26.                 If lLastRow > 1 Then
  27.                     .Range("a2:o" & lLastRow).ClearContents
  28.                 End If

  29.                 .Range("a2").CopyFromRecordset AdoRst
  30.             End With
  31.             MsgBox "查询完毕"
  32.         End If
  33.     End If
  34. End Sub



  35. Function ADOQuery(strFullname As String, Optional strSql As String, Optional blnHasHeader As Boolean = True) As Boolean

  36.     Dim StrConn$

  37.     Set AdoConn = CreateObject("ADODB.Connection")

  38.     Select Case Application.Version
  39.         Case "14.0", "12.0"
  40.             StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & _
  41.                       strFullname & "';Extended Properties='Excel 12.0;HDR=" & blnHasHeader & ";imex=1';"
  42.         Case Else
  43.             StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  44.                       "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=" & blnHasHeader & ";imex=1';"
  45.     End Select

  46.     Debug.Print StrConn

  47.     On Error GoTo ErrorHandler

  48.     With AdoConn
  49.         .CommandTimeout = 5
  50.         .ConnectionTimeout = 5
  51.         .CursorLocation = adUseClient
  52.         .Mode = adModeRead    'Write    'adModeShareDenyWrite
  53.         .ConnectionString = StrConn
  54.         .Open
  55.     End With

  56.     Debug.Print strSql
  57.     Set AdoRst = AdoConn.Execute(strSql)

  58.     ADOQuery = True
  59.     Exit Function


  60. ErrorHandler:
  61.     MsgBox Err.Number & vbCrLf & Err.Description
  62.     Set AdoRst = Nothing
  63.     Set AdoConn = Nothing

  64. End Function
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-7-18 22:11 | 显示全部楼层
hwc2ycy 发表于 2013-7-18 22:08

数据是提取出来了,只是没有表头。
回复

使用道具 举报

发表于 2013-7-18 22:16 | 显示全部楼层
改进下,插入一个模块,代码放入模块中,可在工作表中放一按钮,指定宏为提取数据
  1. Const adUseClient = 3
  2. Const adModeShareDenyWrite = 8
  3. Const adModeReadWrite = 3
  4. Const adModeRead = 1

  5. Dim AdoConn As Object, AdoRst As Object

  6. Sub 提取数据()
  7.     Dim sht As Worksheet
  8.     Dim strSql As String
  9.     Dim strCode As String
  10.     Dim lLastRow As Long

  11.     strCode = Application.InputBox("请输入要查找的代码字段值" & vbCrLf & vbCrLf & "请注意大小写", Type:=2)

  12.     If strCode = "False" Then
  13.         MsgBox "没有输入查询字段或没有确定,结束"
  14.         Exit Sub
  15.     End If

  16.     For Each sht In Worksheets
  17.         If sht.Name <> "提取数据" Then
  18.             strSql = strSql & "select * from [" & sht.Name & "$] where 代码='" & strCode & "' union all "
  19.         End If
  20.     Next

  21.     If Len(strSql) = 0 Then Exit Sub

  22.     strSql = Left(strSql, Len(strSql) - 11)

  23.     If ADOQuery(ThisWorkbook.FullName, strSql) Then

  24.         With Worksheets("提取数据")
  25.             lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
  26.             If lLastRow > 1 Then
  27.                 .Range("a2:o" & lLastRow).ClearContents
  28.             End If

  29.             If AdoRst.RecordCount > 0 Then
  30.                 .Range("a2").CopyFromRecordset AdoRst
  31.                 MsgBox "查询完毕" & vbCrLf & vbCrLf & "一共查询到 " & AdoRst.RecordCount & " 条记录", vbInformation + vbOKOnly
  32.             Else
  33.                 MsgBox "没有查询到符合条件的数据" & vbCrLf & "请检查代码字段"
  34.             End If
  35.         End With
  36.     End If
  37. End Sub

  38. Function ADOQuery(strFullname As String, Optional strSql As String, Optional blnHasHeader As Boolean = True) As Boolean

  39.     Dim StrConn$

  40.     Set AdoConn = CreateObject("ADODB.Connection")

  41.     StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  42.               "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=" & blnHasHeader & ";imex=1';"
  43.     On Error GoTo ErrorHandler

  44.     With AdoConn
  45.         .CommandTimeout = 5
  46.         .ConnectionTimeout = 5
  47.         .CursorLocation = adUseClient
  48.         .Mode = adModeRead
  49.         .ConnectionString = StrConn
  50.         .Open
  51.     End With

  52.     Set AdoRst = AdoConn.Execute(strSql)
  53.     ADOQuery = True
  54.     Exit Function

  55. ErrorHandler:
  56.     MsgBox Err.Number & vbCrLf & Err.Description
  57.     Set AdoRst = Nothing
  58.     Set AdoConn = Nothing
  59. End Function
复制代码
回复

使用道具 举报

发表于 2013-7-18 22:17 | 显示全部楼层
张雄友 发表于 2013-7-18 22:11
数据是提取出来了,只是没有表头。

一样都可以的。
回复

使用道具 举报

发表于 2013-7-18 22:18 | 显示全部楼层
张雄友 发表于 2013-7-18 22:11
数据是提取出来了,只是没有表头。

表头我懒得写了,你直接从SHEET1里复制一个过去放第一行就成了。
反正清除的时候第一行不会动。
回复

使用道具 举报

 楼主| 发表于 2013-7-18 22:23 | 显示全部楼层
hwc2ycy 发表于 2013-7-18 22:18
表头我懒得写了,你直接从SHEET1里复制一个过去放第一行就成了。
反正清除的时候第一行不会动。

strSql = strSql & "select * from [" & sht.Name & "$] where 代码='" & strCode & "' union all "

现在是查询代码的,如果要改查询工序号,就将代码改成工序号,能否将“ where 代码” 这句

改用通用的Application.InputBox 来可以供选择吗?这样就可以查,产量,工号,组别,等等......
回复

使用道具 举报

发表于 2013-7-18 22:29 | 显示全部楼层
再次改下,标题问题已经解决了。
  1. Const adUseClient = 3
  2. Const adModeShareDenyWrite = 8
  3. Const adModeReadWrite = 3
  4. Const adModeRead = 1

  5. Dim AdoConn As Object, AdoRst As Object

  6. Sub 提取数据()
  7.     Dim sht As Worksheet
  8.     Dim strSql As String
  9.     Dim strCode As String
  10.     Dim lLastRow As Long
  11.     Dim arrTitle()
  12.     Dim i As Long
  13.    
  14.     strCode = Application.InputBox("请输入要查找的代码字段值" & vbCrLf & vbCrLf & "请注意大小写", Type:=2)
  15.     If strCode = "False" Then
  16.         MsgBox "没有输入查询字段或没有确定,结束"
  17.         Exit Sub
  18.     End If

  19.     For Each sht In Worksheets
  20.         With sht
  21.             If .Name <> "提取数据" Then
  22.                 lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
  23.                 strSql = strSql & "select * from [" & sht.Name & "$A1:O" & lLastRow & "] where 代码='" & strCode & "' union all "
  24.             End If
  25.         End With
  26.     Next

  27.     If Len(strSql) = 0 Then Exit Sub
  28.     strSql = Left(strSql, Len(strSql) - 11)

  29.     If ADOQuery(ThisWorkbook.FullName, strSql) Then

  30.         With Worksheets("提取数据")
  31.             Application.ScreenUpdating = False
  32.             .UsedRange.ClearContents
  33.             ReDim arrTitle(1 To AdoRst.Fields.Count)
  34.             
  35.             For i = 1 To UBound(arrTitle)
  36.                 arrTitle(i) = AdoRst.Fields(i - 1).Name
  37.             Next
  38.             
  39.             .Range("a1").Resize(, UBound(arrTitle)).Value = arrTitle
  40.             If AdoRst.RecordCount > 0 Then
  41.                 .Range("a2").CopyFromRecordset AdoRst
  42.                 MsgBox "查询完毕" & vbCrLf & vbCrLf & "一共查询到 " & AdoRst.RecordCount & " 条记录", vbInformation + vbOKOnly
  43.             Else
  44.                 MsgBox "没有查询到符合条件的数据" & vbCrLf & "请检查代码字段"
  45.             End If
  46.             
  47.             Application.ScreenUpdating = True
  48.         End With
  49.     End If
  50. End Sub

  51. Function ADOQuery(strFullname As String, Optional strSql As String, Optional blnHasHeader As Boolean = True) As Boolean

  52.     Dim StrConn$
  53.     Set AdoConn = CreateObject("ADODB.Connection")

  54.     StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  55.               "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=" & blnHasHeader & ";imex=1';"
  56.    
  57.     On Error GoTo ErrorHandler

  58.     With AdoConn
  59.         .CommandTimeout = 5
  60.         .ConnectionTimeout = 5
  61.         .CursorLocation = adUseClient
  62.         .Mode = adModeRead
  63.         .ConnectionString = StrConn
  64.         .Open
  65.     End With

  66.     Set AdoRst = AdoConn.Execute(strSql)
  67.     ADOQuery = True
  68.     Exit Function

  69. ErrorHandler:
  70.     MsgBox Err.Number & vbCrLf & Err.Description
  71.     Set AdoRst = Nothing
  72.     Set AdoConn = Nothing
  73. End Function
复制代码
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-6 04:36 , Processed in 0.424401 second(s), 9 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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