星期三, 五月 21, 2008

Excel表格下面的SQL对比

在excel工具--宏下面--VBA然后在模块中加入模块1,然后显示窗体设计器下面加入一个按钮
Sub 查询()
  Dim MYSTR As String
   Range("A2:AT1500").ClearContents
   Set CONN = CreateObject("adodb.connection")
   CONN.Open "provider=microsoft.jet.oledb.4.0;extended properties=excel 8.0;data source=" & ThisWorkbook.FullName
  ' For I = 1 To Sheets.Count - 2
   '    sq1 = sq1 & "select * from [" & Sheets(I).Name & "$] where 工资级别='6级'" & " UNION "
  ' Next I
  ' sq1 = Left(sq1, Len(sq1) - 7)
  Sq1 = "select * from [" & Sheets(2).Name & "$] where 监管条件 like '%/B'  and b2 not in (select b2 from [" & Sheets(1).Name & "$])"
  [a2].CopyFromRecordset CONN.Execute(Sq1)
  CONN.Close
  Set CONN = Nothing
 End Sub

没有评论: