Sub UpdateBrandAndGroup()
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lastRow1 As Long, lastRow2 As Long, i As Long
Dim brandCode As Variant, brandName As Variant
Dim productGroup As String, category As String
Dim brandCodes As Range, brandNames As Range
Dim dict As Object
' 创建字典对象用于存储品牌编码和品牌名称的映射
Set dict = CreateObject("Scripting.Dictionary")
' 定义工作表
Set ws1 = ThisWorkbook.Sheets("月累计")
Set ws2 = ThisWorkbook.Sheets("匹配表")
' 获取【月累计】和【匹配表】的最后一行
lastRow1 = ws1.Cells(ws1.Rows.Count, "C").End(xlUp).Row
lastRow2 = ws2.Cells(ws2.Rows.Count, "A").End(xlUp).Row
' 设置【匹配表】中品牌编码和品牌名称的范围
Set brandCodes = ws2.Range("A1:A" & lastRow2)
Set brandNames = ws2.Range("C1:C" & lastRow2)
' 将品牌编码和品牌名称填充到字典中
For i = 1 To lastRow2
brandCode = brandCodes.Cells(i, 1).Value
brandName = brandNames.Cells(i, 1).Value
If Not IsEmpty(brandCode) Then
dict.Add brandCode, brandName
End If
Next i
' 循环遍历【月累计】的每一行
For i = 1 To lastRow1
' 获取品牌编码和商品组
brandCode = ws1.Cells(i, "C").Value
productGroup = ws1.Cells(i, "N").Value
category = ws1.Cells(i, "B").Value
' 在字典中查找品牌编码对应的品牌名称
If dict.Exists(brandCode) Then
brandName = dict.Item(brandCode)
Else
brandName = ""
End If
' 根据品牌名称和商品组更新【月累计】的O列
If brandName <> "" Then
ws1.Cells(i, "O").Value = brandName
Else
' 如果没有找到品牌名称,则根据商品组进行判断
If productGroup = "卫浴电器" Then
ws1.Cells(i, "O").Value = "卫浴其他"
ElseIf productGroup = "水家电" Then
ws1.Cells(i, "O").Value = "净水其他"
ElseIf productGroup = "商用电器" Then
ws1.Cells(i, "O").Value = "商用其他"
ElseIf category = "厨卫" And IsEmpty(brandName) Then
ws1.Cells(i, "O").Value = "产业带其他"
ElseIf category = "厨房小家电" And IsEmpty(brandName) Then
ws1.Cells(i, "O").Value = "小家电其他"
End If
End If
Next i
' 释放字典对象
Set dict = Nothing
' 提示操作完成
MsgBox "品牌名称和商品组更新完成!"
End Sub
报错 运行时错误'457':该关键字已经与该集合的一个元素相关联,新手上路,有木有大佬帮忙看看我这是啥毛病啊。
VBA报错 错误‘457‘ 新人求助~
最新推荐文章于 2025-02-20 16:17:16 发布