Excel精英培训网

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

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

[复制链接]
发表于 2013-7-18 22:30 | 显示全部楼层
也可以的,但是可能会有BUG。
对于日期,数值字段时,处理方法略有不同了。

回复

使用道具 举报

 楼主| 发表于 2013-7-18 22:35 | 显示全部楼层
hwc2ycy 发表于 2013-7-18 22:30
也可以的,但是可能会有BUG。
对于日期,数值字段时,处理方法略有不同了。

像要查询工序号时,我改成以下这样,不行的,数值字段时
  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) - 9)


  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
复制代码
回复

使用道具 举报

发表于 2013-7-18 22:49 | 显示全部楼层
本帖最后由 hwc2ycy 于 2013-7-18 22:50 编辑

原来strSql = Left(strSql, Len(strSql) - 11)的11是有用意的,你不要随便改。改了就出错了。
另外,数据型的话就要用LIKE来匹配了。
  1. Sub 提取数据()
  2.     Dim sht As Worksheet
  3.     Dim strSql As String
  4.     Dim strCode As String
  5.     Dim lLastRow As Long
  6.     Dim arrTitle()
  7.     Dim i As Long
  8.    
  9.     strCode = Application.InputBox("请输入要查找的工序号字段值" & vbCrLf & vbCrLf & "请注意大小写", Type:=2)
  10.     If strCode = "False" Then
  11.         MsgBox "没有输入查询字段或没有确定,结束"
  12.         Exit Sub
  13.     End If

  14.     For Each sht In Worksheets
  15.         With sht
  16.             If .Name <> "提取数据" Then
  17.                 lLastRow = .Cells(Rows.Count, 1).End(xlUp).Row
  18.                 strSql = strSql & "select * from [" & sht.Name & "$A1:O" & lLastRow & "] where 工序号 like '" & strCode & "' union all "
  19.             End If
  20.         End With
  21.     Next

  22.     If Len(strSql) = 0 Then Exit Sub
  23.     strSql = Left(strSql, Len(strSql) - 11)


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

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

使用道具 举报

 楼主| 发表于 2013-7-18 22:58 | 显示全部楼层
hwc2ycy 发表于 2013-7-18 22:49
原来strSql = Left(strSql, Len(strSql) - 11)的11是有用意的,你不要随便改。改了就出错了。
另外,数据型 ...

出错的,3001类型。

多表提取数据SQL.rar

49.09 KB, 下载次数: 7

回复

使用道具 举报

发表于 2013-7-18 23:04 | 显示全部楼层
你把上面定义的常量搞丢了,肯定要出错呀。
回复

使用道具 举报

发表于 2013-7-18 23:04 | 显示全部楼层
  1. Const adUseClient = 3
  2. Const adModeShareDenyWrite = 8
  3. Const adModeReadWrite = 3
  4. Const adModeRead = 1
复制代码
这几句要放在模块顶部。
回复

使用道具 举报

发表于 2013-7-18 23:05 | 显示全部楼层
  1. Dim AdoConn As Object, AdoRst As Object
复制代码
还有这个模块级变量,放在常量声明下面
回复

使用道具 举报

发表于 2013-7-18 23:06 | 显示全部楼层
我自己测的时候都是没问题的。
回复

使用道具 举报

 楼主| 发表于 2013-7-18 23:13 | 显示全部楼层
hwc2ycy 发表于 2013-7-18 23:05
还有这个模块级变量,放在常量声明下面

原来strSql = Left(strSql, Len(strSql) - 11)的11是有用意的,

11不用改的?11表示什么?
回复

使用道具 举报

发表于 2013-7-18 23:15 | 显示全部楼层    本楼为最佳答案   
生成的SQL语句时,最后的“ union all "是多余的,不需要,11个字符串。
所以用LEFT截取时要减掉11个。
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-6 02:10 , Processed in 0.252207 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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