Sub UpdateDataInFolder()
Dim folderPath As String, filePath As String
Dim sozSheet As Worksheet, currentSheet As Worksheet
Dim sozRange As Range, currentRange As Range
Dim sozCell As Range, currentCell As Range
Set sozSheet = ThisWorkbook.Sheets("Soz") '获取Soz表
Set sozRange = sozSheet.Range("A1").CurrentRegion.Offset(1) '获取Soz表数据区域(排除表头)
folderPath = "C:\Users\username\Desktop\test" '需要替换为你实际的文件夹路径
filePath = Dir(folderPath & "\*.xlsx") '查找文件夹内的所有xlsx文件
Application.ScreenUpdating = False '关闭屏幕更新,加快执行速度
Do Until filePath = ""
Set currentSheet = Workbooks.Open(folderPath & "\" & filePath).Sheets(1) '打开文件,并获取第一个Sheet
Set currentRange = currentSheet.Range("B1").CurrentRegion.Offset(1) '获取当前Sheet的数据区域(排除表头)
For Each sozCell In sozRange.Cells '遍历Soz表的A列单元格
Set currentCell = currentRange.Find(what:=sozCell.Value, LookIn:=xlValues, lookat:=xlWhole) '在当前Sheet的B列中查找Soz表的A列内容
If Not currentCell Is Nothing Then '如果找到了相应的内容
currentCell.Offset(0, 2).Value = sozCell.Offset(0, 1).Value '将对应的D列=Soz表的B列
End If
Next sozCell
currentSheet.Save '保存当前表格
currentSheet.Close '关闭当前表格,释放内存
filePath = Dir() '查找下一个文件
Loop
Application.ScreenUpdating = True '打开屏幕更新
End Sub
上述代码具体操作为:先获取Soz表的A列内容和对应的D列的值,然后遍历指定文件夹内的所有xlsx文件,在每个文件中查找对应的值并修改相应的D列。最后保存修改并关闭文件。请根据实际需要修改代码中的文件夹路径、Soz表的名称和数据区域的范围