You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA实现跨工作簿匹配列数据并复制至空白列

跨工作簿数据匹配与复制需求及现有VBA代码

需求说明

本人拥有order和database两个工作簿,需编写VBA脚本:匹配两个工作簿中B列与G列的行数据,将order工作簿中的指定单元格内容复制到database工作簿的下一个空白列。

现有VBA代码

Sub macro1()

Dim w1 As Worksheet, w2 As Worksheet
Dim iLastRow As Long, r As Long
Dim dict As Object, key As String, ar
Set dict = CreateObject("Scripting.Dictionary")

' 从order工作表(itimat)构建查找字典
Set w2 = Workbooks("Itimat-Bestellung.xlsm").Worksheets("itimat")
iLastRow = w2.Cells(Rows.Count, "B").End(xlUp).Row
ar = w2.Range("B4:D" & iLastRow).Value2

For r = 1 To UBound(ar)
    key = ar(r, 1) & vbTab & ar(r, 2)
    If Len(key) > 1 Then ' 跳过空白行
        If dict.exists(key) Then
            MsgBox "发现重复键值: '" & key & "'", vbCritical, "行号: " & r
            Exit Sub
        Else
            dict.Add key, ar(r, 3)
        End If
    End If
Next

' 更新database工作表(datenbank)
Set w1 = Workbooks("Itimat-Bestellung.xlsm").Worksheets("datenbank")
iLastRow = w1.Cells(Rows.Count, "A").End(xlUp).Row
For r = 2 To iLastRow
    key = w1.Cells(r, "A") & vbTab & w1.Cells(r, "B")
    If dict.exists(key) Then
        w1.Cells(r, "C") = dict(key)
    End If
Next
MsgBox "数据已保存至数据库工作表"
End Sub

辅助参考信息

工作表截图

工作表截图

数据示例

11011Chicken Dönerspieß Kg.5Stk.24320Hot Wings (85-95Stk.) 5 KgBtl.
11012Chicken Dönerspieß Kg.Stk.29510Steak Bites (65-75Stk.) 5 KgBtl.
11010Chicken Dönerspieß Kg.5Stk.21310Tender Filet (145-155Stk.) 5 KgBtl.
11025Chickenburger (11x990g)999Krt63210Coca Cola PET (12x0,5l)Pck
11060Chicken Schnitzel Prem. (3Kg)22x140g777Krt63230Fanta PET (12x0,5l)Pck
11080Chicken Nuggets Prem. (16x700g)Krt63240Sprite PET (12x0,5l)Pck
12025Rind Hamburger 45g (100 Stk.)7Krt63250Mezzo Mix PET (12x0,5l)Pck
13015Pommes 9mm (4x2,5kg)Krt63260Lift Apfelschorle PET (12x0,5l)Pck
31081Falafel 12x1 KgKrt63280Coca Cola Zero PET (12x0,5l)Pck
14010Hamburger Brot 57g (48 Stk)9Krt63610FuzeTea Pfirsich PET (12x0,5l)Pck
14320Tortilla Brot 25cm (18 Stk.)Krt63630FuzeTea Hibiskus PET (12x0,5l)Pck

内容的提问来源于stack exchange,提问作者John Kater

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 06:37:09