|
代码如下。。。
Sub UpdateSKUList()
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
' 设置源工作簿的文件路径
' 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"))
' 设置目标工作表
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)
' 遍历目标工作表的B列,查找匹配的SKU值并写入AD列
For i = 2 To lastRow ' 假设从第2行开始,因为通常第一行是标题行
' If IsEmpty(targetWorksheet.Cells(i, "Q")) And Not IsEmpty(targetWorksheet.Cells(i, "I")) Then
skuValue = Application.Match(arr(i, 2) & arr(i, 3), Application.Index(brr, , 1), 0)
If Not IsError(skuValue) Then
crr(i - 1, 1) = brr(skuValue, 3)
End If
' End If
Next i
targetWorksheet.[ad2].Resize(lastRow - 1) = crr
' 关闭源工作簿,不保存更改
sourceWorkbook.Close SaveChanges:=False
Beep
Application.ScreenUpdating = True
End Sub
|
|