|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
几千个工作簿建议使用ADO法实现更新,否则仅打开工作簿这个动作就要很长时间:- Sub replace()
- Dim myDialog As FileDialog, oFile As Object
- Dim FSO, myFolder As Object, myFiles As Object
- Dim fn$, cnn As Object, m&, SQL$
- Set FSO = CreateObject("Scripting.FileSystemObject")
- Set myDialog = Application.FileDialog(msoFileDialogFolderPicker)
- With myDialog
- If .Show <> -1 Then Exit Sub
- Set myFolder = FSO.GetFolder(.InitialFileName)
- Set myFiles = myFolder.Files
- For Each oFile In myFiles
- If oFile.Name <> ThisWorkbook.Name Then
- m = m + 1
- If m = 1 Then
- Set cnn = CreateObject("adodb.connection")
- cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & oFile
- SQL = "update [Sheet1$] set 单价=100 where 名称='AAA'"
- Else
- SQL = "update [Excel 8.0;Database=" & oFile & "].[Sheet1$] set 单价=100 where 名称='AAA'"
- End If
- cnn.Execute SQL
- End If
- Next
- If m > 0 Then
- MsgBox "文件处理完成"
- cnn.Close
- Set cnn = Nothing
- Else
- MsgBox "没有发现可更新的文件"
- End If
- End With
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|