VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > vb >
  • excel vba教程之不打开工作薄在指定文件夹内取数

1.  不打开工作薄在指定文件夹内取数  by:northwolves

对于固定格式,固定表名的工作薄的汇总,还是建议选择ADO+SQL.假如文件夹内有成百上千个表,由于不必逐个在前台打开工作薄,ADO将表现出其绝对的优势,参考以下代码:

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Dim f As String, n As Long, n0 As Long
[a6:r65536] = ""
n = 6
n0 = 6
'On Error Resume Next
f = Dir(ThisWorkbook.Path & "\*.xls")
With CreateObject("ADODB.Connection")
While f > ""
If Not f = "查询.xls" Then
.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;Hdr=No;imex=1';Data Source=" & ThisWorkbook.Path & "\" & f
Cells(n0, 2).CopyFromRecordset .Execute("select * from [应收+实收$a7:r100] where f1='" & [b2] & "' or f4='" & [b2] & "'")
n = [b65536].End(xlUp).Row + 1
Cells(n0, 1).Resize(n - n0, 1) = .Execute("select * from [应收+实收$b3:b3]")(0)
n0 = n
.Close
End If
f = Dir
Wend
End With
[a6:r65536].Sort [a6]
    Range("cx6:cx22").Select
    Selection.NumberFormatLocal = "m-d;@"
    Range("A6").Select
    Application.ScreenUpdating = True
End Sub

相关教程