|
yah760911 发表于 2012-5-14 08:23
版主,我有多个象“表”这样的文件夹,结果运行不了。可否改下可以执行多个文件夹的语句 - Sub Macro1()
- Dim cnn As Object, cat As Object, tb1 As Object
- Dim SQL$, s$, t$, arr, i&, j&, m&, n&, arrf(), MyPath$, MyFile$
- arr = [a1].CurrentRegion
- Set cnn = CreateObject("adodb.connection")
- Set cat = CreateObject("ADOX.Catalog")
- MyPath = ThisWorkbook.Path & ""
- MyFile = Dir(MyPath, vbDirectory)
- Do While MyFile <> ""
- If MyFile <> "." And MyFile <> ".." Then
- If (GetAttr(MyPath & MyFile) And vbDirectory) = vbDirectory Then
- m = m + 1
- ReDim Preserve arrf(m)
- arrf(m) = MyPath & MyFile & ""
- End If
- End If
- MyFile = Dir
- Loop
- For j = 1 To m
- MyFile = Dir(arrf(j) & "*.xls")
- While MyFile <> ""
- n = n + 1
- If n = 1 Then cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=excel 8.0;Data Source=" & arrf(j) & MyFile
- cat.ActiveConnection = "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties='Excel 8.0;HDR=No';Data Source=" & arrf(j) & MyFile
- For Each tb1 In cat.Tables
- If tb1.Type = "TABLE" Then
- s = Replace(tb1.Name, "'", "")
- If Right(s, 1) = "$" Then
- If n > 1 Then t = "[Excel 8.0;Database=" & arrf(j) & MyFile & "].[" & s & "]" Else t = "[" & s & "]"
- For i = 2 To UBound(arr)
- SQL = "update " & t & " set 子件規格='" & arr(i, 3) & "',基本用量=" & arr(i, 5) & ",子件顏色='" & arr(i, 6) & "' where 子件編碼='" & arr(i, 1) & "'" '只取第一个条件
- cnn.Execute SQL
- Next
- End If
- End If
- Next
- MyFile = Dir()
- Wend
- Next
- cnn.Close
- Set cnn = Nothing
- Set cat = Nothing
- Set tb1 = Nothing
- MsgBox "更新完毕"
- End Sub
复制代码 |
|