Excel精英培训网

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

[已解决]求助!VBA多条件查询数据??

[复制链接]
发表于 2013-12-29 02:46 | 显示全部楼层 |阅读模式
本帖最后由 freexun_zp 于 2013-12-31 21:16 编辑

老师们! 你们好!
            最近遇见一个难题,怎么想也想不通,故来请老师们帮忙解惑,非常感谢!!‘
问题是这样的:
       1.       我要做一个VBA多条件查询EXCEL中原始数据资料,需要查询条件有日期时间,工号,姓名(如附件)。
             但是条件中有一个日期,时间起始条件, 原始资料中日期时间在EXCEL中的不同列显示,如何使用VBA查询时让原始数据资料中的日期时间合并并且达到需要查询资料???(不修改原始资料)

      2.条件查询时有部分人员,工号是不需要提取的,如何让VBA查询时避开那部分人员(人员清单在表2) 达到查询要求??

以上求助各位老师帮忙解答,学生万分感谢!!!


最佳答案
2013-12-29 15:32
改进下,查询的日期时间可有可无,条件任意组合。
  1. Option Explicit
  2. Const adUseClient = 3
  3. Const adModeRead = 1
  4. Const adStateOpen = 1
  5. Dim AdoConn As Object, AdoRst As Object
  6. Function OpenConnect(strFullname) As Boolean
  7. '---------------------------------------------------------------------------------------
  8. ' Procedure : OpenConnect
  9. ' Author    : hwc2ycy
  10. ' Date      : 2013-12-29
  11. ' Purpose   : 建立数据链接,链接成功则返回TRUE,再次执行时,如果链接正常则不再重新建立。
  12. '---------------------------------------------------------------------------------------
  13. '
  14.     Dim StrConn$
  15.     On Error GoTo ErrorHandler
  16.     If Not AdoConn Is Nothing Then
  17.         If (AdoConn.State And adStateOpen) = adStateOpen Then
  18.             OpenConnect = True
  19.             Exit Function
  20.         Else
  21.             Set AdoConn = Nothing
  22.         End If
  23.     End If

  24.     Set AdoConn = CreateObject("ADODB.Connection")
  25.     Select Case Application.Version
  26.         Case "14.0", "12.0", "16.0"
  27.             StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & _
  28.                       strFullname & "';Extended Properties='Excel 12.0;HDR=YES" & ";imex=1';"
  29.         Case Else
  30.             StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  31.                       "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=YES" & ";imex=1';"
  32.     End Select

  33.     With AdoConn
  34.         .CommandTimeout = 5    '命令超时
  35.         .ConnectionTimeout = 5  '连接超时
  36.         .CursorLocation = adUseClient   '光标类型
  37.         .Mode = adModeRead  '模式
  38.         .ConnectionString = StrConn
  39.         .Open
  40.     End With
  41.     OpenConnect = True
  42.     Exit Function
  43. ErrorHandler:
  44.     MsgBox Err.Number & vbCrLf & Err.Description
  45.     Set AdoRst = Nothing
  46. End Function

  47. Sub Main()
  48.     Dim strDatabase$
  49.     Dim strSQL$, strID$, strName$
  50.     Dim strSubSQL$, strDate$
  51.     On Error GoTo ErrorHandler
  52.     strDatabase = ThisWorkbook.FullName
  53.     With Worksheets("异常人员查询")

  54.         If Not OpenConnect(strDatabase) Then
  55.             MsgBox "访问 " & strDatabase & " 失败" & vbCrLf & _
  56.                    "确定退出", vbCritical + vbOKOnly
  57.             Exit Sub
  58.         End If
  59.         '建立连接

  60.         Set AdoRst = CreateObject("adodb.recordset")
  61.         strSQL = "select 單位名稱,班別,人員,工號,姓名,卡鐘代號,卡鐘位置,`進/出`,狀態,刷卡日期,時間,卡號 from [系統導出的出勤資料$a1:n" & Worksheets("系統導出的出勤資料").Cells(Rows.Count, 1).End(xlUp).Row & "] where "

  62.         If Len(.Range("c3").Value) Then
  63.             strDate = "刷卡日期+時間>=#" & .Range("c3").Value & "# and "
  64.             '日期起始时间
  65.         End If

  66.         If Len(.Range("f3").Value) Then
  67.             strDate = strDate & "刷卡日期+時間<=#" & .Range("f3").Value & "# and "
  68.             '日期结束时间
  69.         End If
  70.         
  71.         strSQL = strSQL & strDate
  72.         '此种语句下要求C3是起始时间,F3是结束时间
  73.         
  74.         If Len(.[c5].Value) Then
  75.             strID = "工號 like '" & .[c5].Value & "' and "
  76.             '检测是否指定查询工号
  77.             strSQL = strSQL & strID
  78.         End If

  79.         If Len(.[g5].Value) Then
  80.             strName = "姓名 like '" & .[g5].Value & "' and "
  81.             '检测是否指定查询姓名
  82.             strSQL = strSQL & strName
  83.         End If

  84.         strSubSQL = "工號 not in (select 工號 from [不需要計算的人員$a1:a" & Worksheets("不需要計算的人員").Cells(Rows.Count, 1).End(xlUp).Row & "])"
  85.         '嵌套查询,检测工号是号在排除名单之中

  86.         strSQL = strSQL & strSubSQL
  87.         'Debug.Print strSQL
  88.         AdoRst.Open strSQL, AdoConn, 2, 1

  89.         MsgBox "完成" & vbCrLf & "一共 " & AdoRst.RecordCount & " 条记录"
  90.         Application.ScreenUpdating = False
  91.         If .Cells(Rows.Count, 2).End(xlUp).Row > 9 Then
  92.             .Range("b9:m" & .Cells(Rows.Count, 2).End(xlUp).Row).ClearContents
  93.         End If
  94.         '清空单元格中原有内容
  95.         .Range("b9").CopyFromRecordset AdoRst
  96.         '查询结果写入单元格中
  97.         .Columns("l").NumberFormatLocal = "[$-F400]h:mm:ss AM/PM"
  98.         'L列格式调整为时间
  99.         Application.ScreenUpdating = True
  100.     End With
  101.     AdoRst.Close
  102.     Exit Sub
  103. ErrorHandler:
  104.     MsgBox Err.Number & vbCrLf & _
  105.            Err.Description
  106.     AdoConn.Close
  107.     Set AdoConn = Nothing
  108. End Sub
复制代码

查询条件

查询条件

不需要查询的人员清单

不需要查询的人员清单

刷卡异常查询.rar

100.56 KB, 下载次数: 48

excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2013-12-29 08:13 | 显示全部楼层
哈哈,看来不需要查询的人肯定就是领导了
回复

使用道具 举报

发表于 2013-12-29 08:45 | 显示全部楼层
把你查询的情况说详细一点,免得别人少写了.
比如说:按时间查\按时间和工号查\按时间姓名查\按工号查\按姓名查
回复

使用道具 举报

发表于 2013-12-29 11:46 | 显示全部楼层
  1. Option Explicit

  2. Const adUseClient = 3
  3. Const adModeShareDenyWrite = 8
  4. Const adModeReadWrite = 3
  5. Const adModeRead = 1

  6. Dim AdoConn As Object, AdoRst As Object

  7. Function OpenConnect(strFullname) As Boolean
  8.     Dim StrConn$

  9.     On Error GoTo ErrorHandler
  10.     If AdoConn Is Nothing Then
  11.         Set AdoConn = CreateObject("ADODB.Connection")
  12.     Else
  13.         OpenConnect = True
  14.         Exit Function
  15.     End If

  16.     Select Case Application.Version
  17.         Case "14.0", "12.0", "16.0"
  18.             StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & _
  19.                       strFullname & "';Extended Properties='Excel 12.0;HDR=YES" & ";imex=1';"
  20.         Case Else
  21.             StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  22.                       "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=YES" & ";imex=1';"
  23.     End Select

  24.     With AdoConn
  25.         .CommandTimeout = 5
  26.         .ConnectionTimeout = 5
  27.         .CursorLocation = adUseClient
  28.         .Mode = adModeRead
  29.         .ConnectionString = StrConn
  30.         .Open
  31.     End With

  32.     OpenConnect = True
  33.     Exit Function

  34. ErrorHandler:
  35.     MsgBox Err.Number & vbCrLf & Err.Description
  36.     Set AdoRst = Nothing
  37.     Set AdoConn = Nothing

  38. End Function

  39. Sub Main()
  40.     Dim strDatabase$
  41.     Dim strSQL$

  42.     On Error GoTo ErrorHandler

  43.     strDatabase = ThisWorkbook.FullName
  44.     With Worksheets("异常人员查询")
  45.         If Len(.Range("c3")) = 0 Or Len(.Range("f3").Value) = 0 Then
  46.             MsgBox "C3,F3单元格为必填字段"
  47.             Exit Sub
  48.         End If

  49.         If Not OpenConnect(strDatabase) Then
  50.             MsgBox "访问 " & strDatabase & " 失败" & vbCrLf & _
  51.                    "确定退出", vbCritical + vbOKOnly
  52.             Exit Sub
  53.         End If


  54.         Set AdoRst = CreateObject("adodb.recordset")

  55.         strSQL = "select 單位名稱,班別,人員,工號,姓名,卡鐘代號,卡鐘位置,`進/出`,狀態,刷卡日期,timevalue(時間),卡號 from [系統導出的出勤資料$]"
  56.         Dim strDate$
  57.         strDate = "(cdate(刷卡日期+時間)  between #" & [c3].Value & "# and #" & [f3].Value & "#)"
  58.         Dim strID$, strName$
  59.         strID = "工號 like '" & IIf(Len(.[c5].Value) = 0, "%", .[c5].Value) & "'"
  60.         strName = "姓名 like '" & IIf(Len(.[g5].Value) = 0, "%", .[g5].Value) & "'"
  61.         Dim strSubSql$
  62.         strSubSql = "select 工號 from [不需要計算的人員$]"
  63.         strSQL = strSQL & " where " & strDate & " and " & strID & " and " & strName & " and  工號 not in (" & strSubSql & ")"
  64.         Debug.Print strSQL
  65.         AdoRst.Open strSQL, AdoConn, 2, 1

  66.         With AdoRst
  67.             MsgBox "完成" & vbCrLf & "一共 " & .RecordCount & " 条记录"
  68.         End With

  69.         If .Cells(Rows.Count, 2).End(xlUp).Row > 9 Then
  70.             .Rows("9:" & .Cells(Rows.Count, 2).End(xlUp).Row).Clear
  71.         End If

  72.         .Range("b9").CopyFromRecordset AdoRst
  73.         .Columns("l").NumberFormatLocal = "[$-F400]h:mm:ss AM/PM"

  74.     End With
  75.     Set AdoRst = Nothing
  76.     Exit Sub

  77. ErrorHandler:
  78.     MsgBox Err.Number & vbCrLf & _
  79.            Err.Description
  80.     AdoConn.Close
  81.     Set AdoConn = Nothing
  82. End Sub
复制代码
回复

使用道具 举报

发表于 2013-12-29 11:56 | 显示全部楼层
  1. Const adUseClient = 3
  2. Const adModeRead = 1

  3. Dim AdoConn As Object, AdoRst As Object

  4. Function OpenConnect(strFullname) As Boolean
  5.     Dim StrConn$

  6.     On Error GoTo ErrorHandler
  7.     If AdoConn Is Nothing Then
  8.         Set AdoConn = CreateObject("ADODB.Connection")
  9.     Else
  10.         OpenConnect = True
  11.         Exit Function
  12.     End If

  13.     Select Case Application.Version
  14.         Case "14.0", "12.0", "16.0"
  15.             StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & _
  16.                       strFullname & "';Extended Properties='Excel 12.0;HDR=YES" & ";imex=1';"
  17.         Case Else
  18.             StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  19.                       "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=YES" & ";imex=1';"
  20.     End Select

  21.     With AdoConn
  22.         .CommandTimeout = 5
  23.         .ConnectionTimeout = 5
  24.         .CursorLocation = adUseClient
  25.         .Mode = adModeRead
  26.         .ConnectionString = StrConn
  27.         .Open
  28.     End With

  29.     OpenConnect = True
  30.     Exit Function

  31. ErrorHandler:
  32.     MsgBox Err.Number & vbCrLf & Err.Description
  33.     Set AdoRst = Nothing
  34.     Set AdoConn = Nothing

  35. End Function

  36. Sub Main()
  37.     Dim strDatabase$
  38.     Dim strSQL$
  39.     Dim strID$, strName$
  40.     Dim strSubSql$
  41.     Dim strDate$
  42.     On Error GoTo ErrorHandler

  43.     strDatabase = ThisWorkbook.FullName
  44.     With Worksheets("异常人员查询")
  45.         If Len(.Range("c3")) = 0 Or Len(.Range("f3").Value) = 0 Then
  46.             MsgBox "C3,F3单元格为必填字段"
  47.             Exit Sub
  48.         End If

  49.         If Not OpenConnect(strDatabase) Then
  50.             MsgBox "访问 " & strDatabase & " 失败" & vbCrLf & _
  51.                    "确定退出", vbCritical + vbOKOnly
  52.             Exit Sub
  53.         End If


  54.         Set AdoRst = CreateObject("adodb.recordset")

  55.         strSQL = "select 單位名稱,班別,人員,工號,姓名,卡鐘代號,卡鐘位置,`進/出`,狀態,刷卡日期,timevalue(時間),卡號 from [系統導出的出勤資料$]"

  56.         'strDate = "(cdate(刷卡日期+時間)  between #" & .[c3].Value & "# and #" & .[f3].Value & "#)"
  57.         strDate = "(刷卡日期+時間 between #" & .[c3].Value & "# and #" & .[f3].Value & "#)"

  58.         strID = "工號 like '" & IIf(Len(.[c5].Value) = 0, "%", .[c5].Value) & "'"
  59.         strName = "姓名 like '" & IIf(Len(.[g5].Value) = 0, "%", .[g5].Value) & "'"
  60.         strSubSql = "select 工號 from [不需要計算的人員$]"
  61.         strSQL = strSQL & " where " & strDate & " and " & strID & " and " & strName & " and  工號 not in (" & strSubSql & ")"
  62.         'Debug.Print strSQL
  63.         AdoRst.Open strSQL, AdoConn, 2, 1

  64.         MsgBox "完成" & vbCrLf & "一共 " & AdoRst.RecordCount & " 条记录"
  65.         Application.ScreenUpdating = False
  66.         
  67.         If .Cells(Rows.Count, 2).End(xlUp).Row > 9 Then
  68.             .Range("b9:m" & .Cells(Rows.Count, 2).End(xlUp).Row).ClearContents
  69.         End If

  70.         .Range("b9").CopyFromRecordset AdoRst
  71.         .Columns("l").NumberFormatLocal = "[$-F400]h:mm:ss AM/PM"
  72.         Application.ScreenUpdating = True
  73.     End With
  74.     Set AdoRst = Nothing
  75.     Exit Sub

  76. ErrorHandler:
  77.     MsgBox Err.Number & vbCrLf & _
  78.            Err.Description
  79.     AdoConn.Close
  80.     Set AdoConn = Nothing
  81. End Sub
复制代码
回复

使用道具 举报

发表于 2013-12-29 12:02 | 显示全部楼层
查询的数据列数定好。
  1. Option Explicit
  2. Const adUseClient = 3
  3. Const adModeRead = 1
  4. Dim AdoConn As Object, AdoRst As Object
  5. Function OpenConnect(strFullname) As Boolean
  6.     Dim StrConn$
  7.     On Error GoTo ErrorHandler
  8.     If AdoConn Is Nothing Then
  9.         Set AdoConn = CreateObject("ADODB.Connection")
  10.     Else
  11.         OpenConnect = True
  12.         Exit Function
  13.     End If

  14.     Select Case Application.Version
  15.         Case "14.0", "12.0", "16.0"
  16.             StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & _
  17.                       strFullname & "';Extended Properties='Excel 12.0;HDR=YES" & ";imex=1';"
  18.         Case Else
  19.             StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  20.                       "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=YES" & ";imex=1';"
  21.     End Select

  22.     With AdoConn
  23.         .CommandTimeout = 5
  24.         .ConnectionTimeout = 5
  25.         .CursorLocation = adUseClient
  26.         .Mode = adModeRead
  27.         .ConnectionString = StrConn
  28.         .Open
  29.     End With
  30.     OpenConnect = True
  31.     Exit Function
  32. ErrorHandler:
  33.     MsgBox Err.Number & vbCrLf & Err.Description
  34.     Set AdoRst = Nothing
  35.     Set AdoConn = Nothing
  36. End Function

  37. Sub Main()
  38.     Dim strDatabase$
  39.     Dim strSQL$, strID$, strName$
  40.     Dim strSubSQL$, strDate$
  41.     On Error GoTo ErrorHandler
  42.     strDatabase = ThisWorkbook.FullName
  43.     With Worksheets("异常人员查询")
  44.         If Len(.Range("c3")) = 0 Or Len(.Range("f3").Value) = 0 Then
  45.             MsgBox "C3,F3单元格为必填字段"
  46.             Exit Sub
  47.         End If

  48.         If Not OpenConnect(strDatabase) Then
  49.             MsgBox "访问 " & strDatabase & " 失败" & vbCrLf & _
  50.                    "确定退出", vbCritical + vbOKOnly
  51.             Exit Sub
  52.         End If

  53.         Set AdoRst = CreateObject("adodb.recordset")
  54.         strSQL = "select 單位名稱,班別,人員,工號,姓名,卡鐘代號,卡鐘位置,`進/出`,狀態,刷卡日期,timevalue(時間),卡號 from [系統導出的出勤資料$a:n]"
  55.         'strDate = "(cdate(刷卡日期+時間)  between #" & .[c3].Value & "# and #" & .[f3].Value & "#)"
  56.         strDate = "(刷卡日期+時間 between #" & .[c3].Value & "# and #" & .[f3].Value & "#)"
  57.         strID = "工號 like '" & IIf(Len(.[c5].Value) = 0, "%", .[c5].Value) & "'"
  58.         strName = "姓名 like '" & IIf(Len(.[g5].Value) = 0, "%", .[g5].Value) & "'"
  59.         strSubSQL = "select 工號 from [不需要計算的人員$a:b]"
  60.         strSQL = strSQL & " where " & strDate & " and " & strID & " and " & strName & " and  工號 not in (" & strSubSQL & ")"
  61.         'Debug.Print strSQL
  62.         AdoRst.Open strSQL, AdoConn, 2, 1

  63.         MsgBox "完成" & vbCrLf & "一共 " & AdoRst.RecordCount & " 条记录"
  64.         Application.ScreenUpdating = False
  65.         If .Cells(Rows.Count, 2).End(xlUp).Row > 9 Then
  66.             .Range("b9:m" & .Cells(Rows.Count, 2).End(xlUp).Row).ClearContents
  67.         End If
  68.         .Range("b9").CopyFromRecordset AdoRst
  69.         .Columns("l").NumberFormatLocal = "[$-F400]h:mm:ss AM/PM"
  70.         Application.ScreenUpdating = True
  71.     End With
  72.     Set AdoRst = Nothing
  73.     Exit Sub
  74. ErrorHandler:
  75.     MsgBox Err.Number & vbCrLf & _
  76.            Err.Description
  77.     AdoConn.Close
  78.     Set AdoConn = Nothing
  79. End Sub
复制代码
回复

使用道具 举报

发表于 2013-12-29 15:32 | 显示全部楼层    本楼为最佳答案   
改进下,查询的日期时间可有可无,条件任意组合。
  1. Option Explicit
  2. Const adUseClient = 3
  3. Const adModeRead = 1
  4. Const adStateOpen = 1
  5. Dim AdoConn As Object, AdoRst As Object
  6. Function OpenConnect(strFullname) As Boolean
  7. '---------------------------------------------------------------------------------------
  8. ' Procedure : OpenConnect
  9. ' Author    : hwc2ycy
  10. ' Date      : 2013-12-29
  11. ' Purpose   : 建立数据链接,链接成功则返回TRUE,再次执行时,如果链接正常则不再重新建立。
  12. '---------------------------------------------------------------------------------------
  13. '
  14.     Dim StrConn$
  15.     On Error GoTo ErrorHandler
  16.     If Not AdoConn Is Nothing Then
  17.         If (AdoConn.State And adStateOpen) = adStateOpen Then
  18.             OpenConnect = True
  19.             Exit Function
  20.         Else
  21.             Set AdoConn = Nothing
  22.         End If
  23.     End If

  24.     Set AdoConn = CreateObject("ADODB.Connection")
  25.     Select Case Application.Version
  26.         Case "14.0", "12.0", "16.0"
  27.             StrConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source='" & _
  28.                       strFullname & "';Extended Properties='Excel 12.0;HDR=YES" & ";imex=1';"
  29.         Case Else
  30.             StrConn = "Provider= Microsoft.Jet.OLEDB.4.0;" & _
  31.                       "Data Source='" & strFullname & "';Extended Properties='Excel 8.0;HDR=YES" & ";imex=1';"
  32.     End Select

  33.     With AdoConn
  34.         .CommandTimeout = 5    '命令超时
  35.         .ConnectionTimeout = 5  '连接超时
  36.         .CursorLocation = adUseClient   '光标类型
  37.         .Mode = adModeRead  '模式
  38.         .ConnectionString = StrConn
  39.         .Open
  40.     End With
  41.     OpenConnect = True
  42.     Exit Function
  43. ErrorHandler:
  44.     MsgBox Err.Number & vbCrLf & Err.Description
  45.     Set AdoRst = Nothing
  46. End Function

  47. Sub Main()
  48.     Dim strDatabase$
  49.     Dim strSQL$, strID$, strName$
  50.     Dim strSubSQL$, strDate$
  51.     On Error GoTo ErrorHandler
  52.     strDatabase = ThisWorkbook.FullName
  53.     With Worksheets("异常人员查询")

  54.         If Not OpenConnect(strDatabase) Then
  55.             MsgBox "访问 " & strDatabase & " 失败" & vbCrLf & _
  56.                    "确定退出", vbCritical + vbOKOnly
  57.             Exit Sub
  58.         End If
  59.         '建立连接

  60.         Set AdoRst = CreateObject("adodb.recordset")
  61.         strSQL = "select 單位名稱,班別,人員,工號,姓名,卡鐘代號,卡鐘位置,`進/出`,狀態,刷卡日期,時間,卡號 from [系統導出的出勤資料$a1:n" & Worksheets("系統導出的出勤資料").Cells(Rows.Count, 1).End(xlUp).Row & "] where "

  62.         If Len(.Range("c3").Value) Then
  63.             strDate = "刷卡日期+時間>=#" & .Range("c3").Value & "# and "
  64.             '日期起始时间
  65.         End If

  66.         If Len(.Range("f3").Value) Then
  67.             strDate = strDate & "刷卡日期+時間<=#" & .Range("f3").Value & "# and "
  68.             '日期结束时间
  69.         End If
  70.         
  71.         strSQL = strSQL & strDate
  72.         '此种语句下要求C3是起始时间,F3是结束时间
  73.         
  74.         If Len(.[c5].Value) Then
  75.             strID = "工號 like '" & .[c5].Value & "' and "
  76.             '检测是否指定查询工号
  77.             strSQL = strSQL & strID
  78.         End If

  79.         If Len(.[g5].Value) Then
  80.             strName = "姓名 like '" & .[g5].Value & "' and "
  81.             '检测是否指定查询姓名
  82.             strSQL = strSQL & strName
  83.         End If

  84.         strSubSQL = "工號 not in (select 工號 from [不需要計算的人員$a1:a" & Worksheets("不需要計算的人員").Cells(Rows.Count, 1).End(xlUp).Row & "])"
  85.         '嵌套查询,检测工号是号在排除名单之中

  86.         strSQL = strSQL & strSubSQL
  87.         'Debug.Print strSQL
  88.         AdoRst.Open strSQL, AdoConn, 2, 1

  89.         MsgBox "完成" & vbCrLf & "一共 " & AdoRst.RecordCount & " 条记录"
  90.         Application.ScreenUpdating = False
  91.         If .Cells(Rows.Count, 2).End(xlUp).Row > 9 Then
  92.             .Range("b9:m" & .Cells(Rows.Count, 2).End(xlUp).Row).ClearContents
  93.         End If
  94.         '清空单元格中原有内容
  95.         .Range("b9").CopyFromRecordset AdoRst
  96.         '查询结果写入单元格中
  97.         .Columns("l").NumberFormatLocal = "[$-F400]h:mm:ss AM/PM"
  98.         'L列格式调整为时间
  99.         Application.ScreenUpdating = True
  100.     End With
  101.     AdoRst.Close
  102.     Exit Sub
  103. ErrorHandler:
  104.     MsgBox Err.Number & vbCrLf & _
  105.            Err.Description
  106.     AdoConn.Close
  107.     Set AdoConn = Nothing
  108. End Sub
复制代码
回复

使用道具 举报

 楼主| 发表于 2013-12-30 09:41 | 显示全部楼层
hwc2ycy 发表于 2013-12-29 15:32
改进下,查询的日期时间可有可无,条件任意组合。

这代码太多啊
            重要的是那些呢 ? 求老师解析?VBA不是很好,非常感谢

回复

使用道具 举报

发表于 2013-12-30 09:47 | 显示全部楼层
freexun_zp 发表于 2013-12-30 09:41
这代码太多啊
            重要的是那些呢 ? 求老师解析?VBA不是很好,非常感谢

你会用就成了,主要是利用SQL查询来实现。


回复

使用道具 举报

 楼主| 发表于 2013-12-30 10:53 | 显示全部楼层
hwc2ycy 发表于 2013-12-30 09:47
你会用就成了,主要是利用SQL查询来实现。

老师你好
    我调试了一下你的代码
                      系统提示错误  如图
excel提示无法打开事件   这是什么问题?求老师帮忙在解答一下  或用附件呈现,非常感谢
        老师提供的代码中除函数公式外其他都能理解   “看公式大概意思应是链接外部数据用的”,这部分不会用
我的excel版本是2003版本的   请在帮一下帮,谢谢!



无标题.png
kk.png
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-19 08:07 , Processed in 0.348877 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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