利用Excel VBA SQL做特殊文件浏览器


1、利用JamShellBrowser for ActiveX

 

 
 
  1. '转载请注明:本文来自:Excel吧 (www.excelba.com) 详细出处参考:http://www.excelba.com/Art/Html/104.html  
  2. Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As LongByVal nIndex As LongAs Long 
  3. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As StringByVal lpWindowName As StringAs Long 
  4. Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As LongByVal nIndex As LongByVal dwNewLong As LongAs Long 
  5. Private Const GWL_STYLE = (-16)  
  6. Private Const WS_THICKFRAME As Long = &H40000 '(恢复大小)  
  7. Private Const WS_MINIMIZEBOX As Long = &H20000 '(最小化)  
  8. Private Const WS_MAXIMIZEBOX As Long = &H10000 '(最大化)  
  9.  
  10.  
  11. Private Sub JamShellList1_OnAddItem(ByVal ItemFullPath As String, CanAdd As Boolean)  
  12. Dim i As Integer 
  13. 'Dim rg As Range  
  14. 'Dim ws As Worksheet  
  15. 'bk = Me.JamShellList1.GetFullPath(Item) '=ItemFullPath  
  16. xx = Right(ItemFullPath, Len(ItemFullPath) - InStrRev(ItemFullPath, "\")) 'ItemFullPath字符串\后的  
  17. If InStr(xx, "."Then 
  18.     xx = Left(xx, InStr(xx, ".") - 1)  
  19. End If 
  20. 'Set ws = ThisWorkbook.ActiveSheet  
  21. 'nLenRow = 1000   '总数据列数  
  22. 'nColumn = 1  
  23. bp = False 
  24.     For i = 0 To nLenName - 1  
  25.         'Set rg = ws.Cells(nRow, nColumn)  
  26.         If StrComp(xx, sName(i)) = 0 Then 
  27.             bp = True 
  28.             Exit For 
  29.         End If 
  30.     Next 
  31.     If Not bp Then 
  32.         CanAdd = False 
  33.     End If 
  34.  
  35. 'If StrComp(xx, "CCtalk") = 0 Then  
  36.  
  37.   ' CanAdd = False  
  38.  
  39. 'End If  
  40. 'rg.Activate  
  41.  
  42. End Sub 
  43.  
  44.  
  45. Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)  
  46.   'GoUp & NewFolker  
  47.   If Button.Index = 1 Then 
  48.     JamShellTree1.GoUp  
  49.   End If 
  50.   If Button.Index = 2 Then 
  51.     JamShellList1.CreateDir "New Folder"True 
  52.   End If 
  53.   'History  
  54.   If Button.Index = 5 Then 
  55.     JamShellList1.MoveInHistory -1  
  56.   End If 
  57.   If Button.Index = 6 Then 
  58.     JamShellList1.MoveInHistory 1  
  59.   End If 
  60.   'Viewstyle  
  61.   If Button.Index = 9 Then 
  62.     JamShellList1.ViewStyle = vsIcon  
  63.   End If 
  64.   If Button.Index = 10 Then 
  65.     JamShellList1.ViewStyle = vsList  
  66.   End If 
  67.   If Button.Index = 11 Then 
  68.     JamShellList1.ViewStyle = vsReport  
  69.   End If 
  70.   If Button.Index = 12 Then 
  71.     JamShellList1.Thumbnails = Not JamShellList1.Thumbnails  
  72.   End If 
  73.   'Properties & Delete  
  74.   If Button.Index = 15 Then 
  75.     JamShellList1.InvokeCommandOnSelected "delete" 
  76.   End If 
  77.   If Button.Index = 16 Then 
  78.     JamShellList1.InvokeCommandOnSelected "properties" 
  79.   End If 
  80. End Sub 
  81.  
  82. Private Sub UserForm_Activate()  
  83.   Dim b As Button  
  84.   JamShellTree1.ShellLink = JamShellLink1  
  85.   JamShellList1.ShellLink = JamShellLink1  
  86.   JamShellCombo1.ShellLink = JamShellLink1  
  87.   JamThumbnailImage1.ShellLink = JamShellLink1  
  88.   Toolbar1.ImageList = ImageList1  
  89.   'GoUp & NewFolker  
  90.   Toolbar1.Buttons.Add 1, """", ButtonStyleConstants.tbrDefault, 1  
  91.   Toolbar1.Buttons.Add 2, """", ButtonStyleConstants.tbrDefault, 6  
  92.   Toolbar1.Buttons.Add 3, """", ButtonStyleConstants.tbrPlaceholder, 0  
  93.   Toolbar1.Buttons.Add 4, """", ButtonStyleConstants.tbrPlaceholder, 0  
  94.   'History  
  95.   Toolbar1.Buttons.Add 5, """", ButtonStyleConstants.tbrDefault, 9  
  96.   Toolbar1.Buttons.Add 6, """", ButtonStyleConstants.tbrDefault, 3  
  97.   Toolbar1.Buttons.Add 7, """", ButtonStyleConstants.tbrPlaceholder, 0  
  98.   Toolbar1.Buttons.Add 8, """", ButtonStyleConstants.tbrPlaceholder, 0  
  99.   'Viewstyle  
  100.   Toolbar1.Buttons.Add 9, """", ButtonStyleConstants.tbrDefault, 4  
  101.   Toolbar1.Buttons.Add 10, """", ButtonStyleConstants.tbrDefault, 5  
  102.   Toolbar1.Buttons.Add 11, """", ButtonStyleConstants.tbrDefault, 8  
  103.   Toolbar1.Buttons.Add 12, """", ButtonStyleConstants.tbrDefault, 11  
  104.   Toolbar1.Buttons.Add 13, """", ButtonStyleConstants.tbrPlaceholder, 0  
  105.   Toolbar1.Buttons.Add 14, """", ButtonStyleConstants.tbrPlaceholder, 0  
  106.   'Properties & Delete  
  107.   Toolbar1.Buttons.Add 15, """", ButtonStyleConstants.tbrDefault, 2  
  108.   Toolbar1.Buttons.Add 16, """", ButtonStyleConstants.tbrDefault, 7  
  109.   Toolbar1.Buttons.Add 17, """", ButtonStyleConstants.tbrPlaceholder, 0  
  110.   Toolbar1.Buttons.Add 18, """", ButtonStyleConstants.tbrPlaceholder, 0  
  111. End Sub 
  112. '转载请注明:本文来自:Excel吧 (www.excelba.com) 详细出处参考:http://www.excelba.com/Art/Html/104.html  
  113. Private Sub UserForm_Initialize()  
  114. Dim hWndForm As Long 
  115.   Dim IStyle As Long 
  116.   hWndForm = FindWindow("ThunderDFrame"Me.Caption)  
  117.   IStyle = GetWindowLong(hWndForm, GWL_STYLE)  
  118.   IStyle = IStyle Or WS_THICKFRAME '还原  
  119.   IStyle = IStyle Or WS_MINIMIZEBOX '最小化  
  120.   IStyle = IStyle Or WS_MAXIMIZEBOX '最大化  
  121.   SetWindowLong hWndForm, GWL_STYLE, IStyle  
  122.  
  123. End Sub 

2、利用Excel VBA对筛选后的文件名进行记录

 

 
 
  1. Option Explicit  
  2. Public sName(1000) As String  '2013.1.27 by lijilin  
  3. Public nLenName As Integer    '2013.1.27 by lijilin  
  4.  
  5. Sub Macro1()  
  6. Application.ScreenUpdating = False '程序运行时不刷屏,省时且专业  
  7. Manegement  
  8. UserForm1.JamShellList1.FullRefresh  
  9. Application.ScreenUpdating = True 
  10. End Sub 
  11.  
  12. Sub Macro2()  
  13. Application.ScreenUpdating = False 
  14. UserForm1.Show  
  15. Application.ScreenUpdating = True 
  16. End Sub 
  17. Sub Macro3()  
  18. Application.OnKey "^l""Macro1" 
  19. Application.OnKey "^k""Macro2" 
  20. Application.OnKey "^j""Macro4" 
  21. End Sub 
  22. Sub Macro4()  
  23. Application.ScreenUpdating = False 
  24. UserForm2.Show  
  25. Application.ScreenUpdating = True 
  26. End Sub 
  27. Public Sub Manegement()  '2013.1.27 by lijilin  
  28. 'Dim sName(10) As String  
  29. 'Dim nLenName As Integer  
  30. 'Dim nNumber(10) As Integer  
  31. Dim i As Integer 
  32. Dim rg As Range  
  33. Dim nRow As Integer 
  34. Dim nLenRow As Integer 
  35. Dim nColumn As Integer 
  36. Dim ws As Worksheet  
  37. Application.ScreenUpdating = False '程序运行时不刷屏,省时且专业  
  38. Set ws = ThisWorkbook.ActiveSheet  
  39. nLenRow = 3500   '总数据列数  
  40. nLenName = 0  
  41. nColumn = 1    '统计第1列的数据,名称放到sName()中,数量放到nNumber()中,个数为nLenName  
  42. 'For i = 0 To 10  
  43.     'nNumber(i) = 0  
  44. 'Next  
  45. For nRow = 2 To nLenRow       '从第二行开始扫描数据,第一行为标题名称  
  46.     Set rg = ws.Cells(nRow, nColumn)  
  47.     If Not IsEmpty(rg) And Not rg.Rows.Hidden Then  '将筛选后的文件名统计2013.1.27 by lijilin  
  48.         For i = 0 To nLenName - 1  
  49.             If StrComp(sName(i), rg.Value) = 0 Then 
  50.                 'nNumber(i) = nNumber(i) + 1  
  51.                 Exit For 
  52.             End If 
  53.         Next 
  54.         If i = nLenName Then    '很有意思 2013.1.27 by lijilin  
  55.             sName(nLenName) = rg.Value  
  56.             'nNumber(nLenName) = 1  
  57.             nLenName = nLenName + 1  
  58.         End If 
  59.     End If 
  60. Next 
  61. Application.ScreenUpdating = True 
  62. End Sub 
  63.  

3、利用Excel SQL制作特殊的筛选器

 

 
 
  1. Private Sub CommandButton1_Click()  
  2.   ExSQL (TextBox1.Text)  
  3. End Sub 
  4.  
  5. 'Option Explicit  
  6.  
  7. Function ExSQL(strTemp)  
  8.  
  9. Application.ScreenUpdating = False '关闭屏幕刷新,成对出现,提高速度  
  10. 'Range(Cells(1, 1), Cells(1, 1)).Rows.EntireRow.Hidden = True  
  11. 'Range(Cells(1, 1), Cells(1, 1)).Rows.EntireRow.Hidden = False  
  12. 'Range(Cells(1, 1), Cells(1, 1)).Rows.EntireRow.Delete  
  13. 'Range(Cells(1, 1), Cells(1, 1)).Rows.EntireRow.Insert  
  14. Dim ws As Worksheet  
  15. Dim rg As Range  
  16. Set ws = ThisWorkbook.ActiveSheet  
  17. Dim nEndRow As Integer  '求sheet的行数和列数  
  18. Dim nEndColumn As Integer 
  19. With ws.UsedRange  
  20.     nEndRow = .Rows.Count + .Row - 1  
  21.     nEndColumn = .Columns.Count + .Column - 1  
  22. End With 
  23. Set rg = ws.Range(ws.Cells(1, nEndColumn + 1), ws.Cells(nEndRow, nEndColumn + 1))  
  24. 'rg.Columns.EntireColumn.Insert  
  25. rg = "=row()" 
  26. Dim cn As ADODB.Connection '定义数据库连接  
  27. Set cn = New ADODB.Connection '创建数据库连接  
  28. With cn  
  29. .Provider = "Microsoft.Jet.OLEDB.4.0" 
  30. .ConnectionString = "Data Source=" & ActiveWorkbook.Path & "\" & ActiveWorkbook.Name & ";" & "Extended Properties=Excel 8.0;"  
  31. .Open  
  32. End With 
  33. Dim rs As ADODB.Recordset '定义一个数据集保存数据  
  34. Set rs = New ADODB.Recordset '创建一个数据集保存数据  
  35. rs.ActiveConnection = cn  
  36. Dim strSQL As String '定义SQL语句  
  37.  
  38. strSQL = "select F" & nEndColumn + 1 & " from [" & ws.Name & "$] where " & strTemp  
  39. rs.Open strSQL, , 3, 2 '执行SQL查询 后面不加,,3,2,则指针向前,rs.recordcount=-1****!!!2013.2.4 by lijilin  
  40. 'ThisWorkbook.Sheets(3).[a2].CopyFromRecordset cn.Execute(sql_str)  
  41. '****************SQL得到的结果数据来源于sheet,如果sheet变,SQL得到的结果会跟着变*********  
  42. '****************故要获得正确的结果,数据源sheet不能动!!**********2013.2.4 by lijilin  
  43. '如清空了数据源就得不到正确的结果了!!!!!!!!!**********************************  
  44. 'ThisWorkbook.Sheets(2).Cells.ClearContents       '清理保存数据的区域---调试用  
  45. 'ThisWorkbook.Sheets(2).[a1].CopyFromRecordset rs  
  46. 'ThisWorkbook.Sheets(2).Range("a2").CopyFromRecordset rs '----调试用  
  47. Dim nRow(5000) As Integer 
  48. Dim nLenRow As Integer 
  49. Dim i As Integer 
  50. 'nLenRow = rs.PageSize  
  51. If Not (rs.BOF And rs.EOF) Then    '处理无数据无法移动的情况  
  52.  rs.MoveFirst     '******!!!2013.2.4 by lijilin  
  53. 'If rs.recordcount > 0 Then   '*****?rs.recordcount=-1  rs.Open strSQL指针向前产生  
  54.     i = 0  
  55.     Do While Not rs.EOF  
  56.         nRow(i) = rs.Fields(0).Value  
  57.         rs.MoveNext  
  58.         i = i + 1  
  59.     Loop 
  60.     nLenRow = i  
  61. 'End If  
  62. End If 
  63. 'For i = 0 To rs.Fields.Count - 1  '测试字段名  
  64.   '  ws.Cells(13, i + 2) = rs.Fields(i).Name  
  65. 'Next  
  66. rs.Close  
  67. cn.Close  
  68. 'For i = 0 To nLenRow - 1          '测试查询的值  
  69.  '   ws.Cells(14, i + 2) = nRow(i)  
  70. 'Next  
  71. rg.Columns.EntireColumn.Delete  
  72.  
  73. Dim j As Integer 
  74. For i = 2 To nEndRow  
  75.     Set rg = ws.Range(ws.Cells(i, 1), ws.Cells(i, 1))  
  76.     For j = 0 To nLenRow - 1  
  77.         If i = nRow(j) Then 
  78.             Exit For 
  79.         End If 
  80.     Next 
  81.     If j = nLenRow Then 
  82.         rg.Rows.EntireRow.Hidden = True 
  83.     Else 
  84.         rg.Rows.EntireRow.Hidden = False 
  85.     End If 
  86. Next 
  87. Application.ScreenUpdating = True '打开屏幕刷新,成对出现,提高速度  
  88. End Function 
  89.  

4、当workbook打开时将快捷键导入

 

 
 
  1. Private Sub Workbook_Open()  
  2.   'UserForm1.Show  
  3.   'Application.OnKey "^l", "Macro2"  
  4.   Macro3  
  5. End Sub 

5、不同工作薄数据拷贝代码——自动办公代码

 

 
 
  1. Sub M2()  
  2. '  
  3. ' Macro1 Macro  
  4. '  
  5.  
  6. '  
  7. Dim wb As Workbook  
  8. Set wb = Application.Workbooks(2)  
  9. Dim rg As Range  
  10. Dim ws As Worksheet  
  11. Set ws = wb.Worksheets(1)  
  12. Set rg = ws.Range("Z445:Z979")  
  13. 'rg.Select  
  14. rg.Copy  
  15.  
  16. Tabelle2.Range("AE445:AE979").PasteSpecial  
  17.  
  18.  
  19. End Sub 
  20.  
  21. Sub M3()  
  22. '  
  23. ' Macro1 Macro  
  24. '  
  25.  
  26. '  
  27. Dim wb As Workbook  
  28. Set wb = Application.Workbooks(2)  
  29. Dim ws As Worksheet  
  30. Set ws = wb.Worksheets(1)  
  31. 'ws.Range("A1").Select  
  32. For i = 1 To 1758  
  33.     ws.Range("AG" & i & ":CD" & i).Copy  
  34.     Tabelle2.Range("AT" & i & ":CQ" & i).PasteSpecial  
  35. Next 
  36.  
  37.  
  38. End Sub 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值