自用代碼實(shí)現(xiàn)Xlookup
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表數(shù)據(jù)區(qū)域(排除表頭)
? ??
? ? folderPath = "C:\Users\username\Desktop\test" '需要替換為你實(shí)際的文件夾路徑
? ? filePath = Dir(folderPath & "\*.xlsx") '查找文件夾內(nèi)的所有xlsx文件
? ??
? ? Application.ScreenUpdating = False '關(guān)閉屏幕更新,加快執(zhí)行速度
? ??
? ? Do Until filePath = ""
? ? ? ? Set currentSheet = Workbooks.Open(folderPath & "\" & filePath).Sheets(1) '打開文件,并獲取第一個(gè)Sheet
? ? ? ? Set currentRange = currentSheet.Range("B1").CurrentRegion.Offset(1) '獲取當(dāng)前Sheet的數(shù)據(jù)區(qū)域(排除表頭)
? ? ? ??
? ? ? ? For Each sozCell In sozRange.Cells '遍歷Soz表的A列單元格
? ? ? ? ? ? Set currentCell = currentRange.Find(what:=sozCell.Value, LookIn:=xlValues, lookat:=xlWhole) '在當(dāng)前Sheet的B列中查找Soz表的A列內(nèi)容
? ? ? ? ? ? If Not currentCell Is Nothing Then '如果找到了相應(yīng)的內(nèi)容
? ? ? ? ? ? ? ? currentCell.Offset(0, 2).Value = sozCell.Offset(0, 1).Value '將對(duì)應(yīng)的D列=Soz表的B列
? ? ? ? ? ? End If
? ? ? ? Next sozCell
? ? ? ??
? ? ? ? currentSheet.Save '保存當(dāng)前表格
? ? ? ? currentSheet.Close '關(guān)閉當(dāng)前表格,釋放內(nèi)存
? ? ? ? filePath = Dir() '查找下一個(gè)文件
? ? Loop
? ??
? ? Application.ScreenUpdating = True '打開屏幕更新
End Sub
上述代碼具體操作為:先獲取Soz表的A列內(nèi)容和對(duì)應(yīng)的D列的值,然后遍歷指定文件夾內(nèi)的所有xlsx文件,在每個(gè)文件中查找對(duì)應(yīng)的值并修改相應(yīng)的D列。最后保存修改并關(guān)閉文件。請(qǐng)根據(jù)實(shí)際需要修改代碼中的文件夾路徑、Soz表的名稱和數(shù)據(jù)區(qū)域的范圍。