|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
代码如下。。。。
Sub UpdateSKUList2() 'https://club.excelhome.net/thread-1701578-2-1.html 楼主13楼需要修改
t = Timer
Application.ScreenUpdating = False
Dim sourceWorkbook As Workbook
Dim targetWorksheet As Worksheet
Dim sourceWorksheet As Worksheet
Dim lastRow As Long
Dim i As Long
Dim skuValue As Variant
Dim sourceFilePath As String
Set d = CreateObject("scripting.dictionary")
' 设置源工作簿的文件路径
' sourceFilePath = "P:\PlG Files\test.xlsb"
sourceFilePath = "K:\excelhome\test.xlsb"
' 以只读方式打开源工作簿
Set sourceWorkbook = Workbooks.Open(sourceFilePath, ReadOnly:=True)
Set sht = sourceWorkbook.Worksheets("Database")
brr = Intersect(sht.UsedRange, sht.Columns("a:c"))
For i = 1 To UBound(brr)
If brr(i, 1) <> Empty Then d(brr(i, 1)) = i
Next
' 设置目标工作表
Set targetWorksheet = ThisWorkbook.Worksheets("LIST")
' 获取目标工作表的最后一行
With targetWorksheet
lastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
c = .Cells(1, .Columns.Count).End(1).Column
arr = .Range(.[a1], .Cells(lastRow, c))
End With
ReDim crr(1 To lastRow - 1, 1 To 1)
Set vendor = ThisWorkbook.Worksheets("Vendor")
drr = vendor.UsedRange
' 遍历目标工作表的B列,查找匹配的SKU值并写入AD列
For i = 2 To lastRow ' 假设从第2行开始,因为通常第一行是标题行
If d.exists(arr(i, 2) & arr(i, 3)) Then
crr(i - 1, 1) = Application.VLookup(brr(d(arr(i, 2) & arr(i, 3)), 3), drr, 2, 0)
End If
Next i
targetWorksheet.[v2].Resize(lastRow - 1) = crr
' 关闭源工作簿,不保存更改
sourceWorkbook.Close SaveChanges:=False
MsgBox "共耗时: " & Timer - t
Application.ScreenUpdating = True
Set d = Nothing
End Sub
|
评分
-
1
查看全部评分
-
|