本帖最后由 Caraciold_Jr 于 2023-7-16 22:42 编辑
[Visual Basic] 纯文本查看 复制代码 Sub ReplaceA3InAllFiles()
Dim folderPath As String
Dim fileName As String
Dim wb As Workbook
Dim ws As Worksheet
Dim targetCell As Range
Dim newDate As Date
' 指定文件夹路径
folderPath = "C:\path\to\your\prog\folder\"
' 设置新的日期值
newDate = DateSerial(2023, 7, 8)
' 检查路径末尾是否有反斜杠
If Right(folderPath, 1) <> "\" Then folderPath = folderPath & "\"
' 获取文件夹中的第一个Excel文件名
fileName = Dir(folderPath & "*.xls*")
' 遍历文件夹中的所有Excel文件
Do While fileName <> ""
' 打开工作簿
Set wb = Workbooks.Open(folderPath & fileName)
' 遍历工作簿中的所有工作表
For Each ws In wb.Worksheets
' 设置目标单元格
Set targetCell = ws.Range("A3")
' 替换目标单元格的数据
targetCell.Value = newDate
Next ws
' 保存并关闭工作簿
wb.Close SaveChanges:=True
' 获取文件夹中的下一个Excel文件名
fileName = Dir
Loop
End Sub
|