我有2本工作簿第1册和第2册。
第1册有3个填充列。
第2册有2个填充列。
首先,我通过比较两本书的样式编号,从第1册到第2册导入信息,乐队编号。
当两本书中的样式编号匹配时,书籍1中的编号将导入第2册。
这是代码:
Sub procedure2()
Dim key As Variant, oCell As Range, i&, z%
Dim w1 As Worksheet, w2 As Worksheet
Dim Dic As Object: Set Dic = CreateObject("Scripting.Dictionary")
Dim Dic2 As Object: Set Dic2 = CreateObject("Scripting.Dictionary")
'source
Set w1 = Workbooks("book1.xlsm").Worksheets(1)
'destination
Set w2 = Workbooks("book2.xlsm").Worksheets(1)
'-------------------------------------------------------------------------
'get the last row for w1
i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row
'-------------------------------------------------------------------------
' fill dictionary with data for searching
For Each oCell In w1.Range("C2:C" & i)
'row number for duplicates
z = 1: While Dic.exists(oCell.Value & "_" & z): z = z + 1: Wend
'add data with row number to dictionary
If Not Dic.exists(oCell.Value & "_" & z) Then
Dic.Add oCell.Value & "_" & z, oCell.Offset(, -2).Value
End If
Next
'-------------------------------------------------------------------------
'get the last row for w2
i = w2.Cells.SpecialCells(xlCellTypeLastCell).Row
'-------------------------------------------------------------------------
'fill "B" with results
For Each oCell In w2.Range("D2:D" & i)
'determinate row number for duplicated values
z = 1: While Dic2.exists(oCell.Value & "_" & z): z = z + 1: Wend
'search
For Each key In Dic
If oCell.Value & "_" & z = key Then
oCell.Offset(, -2).Value = Dic(key)
End If
Next
'correction of the dictionary in case
'when sheet "A" has less duplicates than sheet "B"
If oCell.Offset(, -2).Value = "" Then
Dic2.RemoveAll: z = 1
For Each key In Dic
If oCell.Value & "_" & z = key Then
oCell.Offset(, -2).Value = Dic(key)
End If
Next
End If
'add to dictionary already passed results for
'the next duplicates testing
If Not Dic2.exists(oCell.Value & "_" & z) Then
Dic2.Add oCell.Value & "_" & z, ""
End If
Next
End Sub
它成功地运作。
但现在我想通过比较第1册和第2册中包含的样式编号和采购订单编号来导入信息,编号。
如果两本图书的样式编号匹配且两本图书的采购订单编号与信息匹配,则应导入相关的编号。
如何修改代码才能执行此操作?
答案 0 :(得分:0)
我希望这是你要找的东西吗? 您需要匹配两列,以便将两列都放到字典中。
'.......
'-------------------------------------------------------------------------
'get the last row for w1
i = w1.Cells.SpecialCells(xlCellTypeLastCell).Row
'-------------------------------------------------------------------------
' fill dictionary with data for searching
For Each oCell In w1.Range("C2:C" & i)
'row number for duplicates
z = 1: While Dic.exists(oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z): z = z + 1: Wend
'add data with row number to dictionary
If Not Dic.exists(oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z) Then
Dic.Add oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z, oCell.Offset(, -2).Value
End If
Next
'-------------------------------------------------------------------------
'get the last row for w2
i = w2.Cells.SpecialCells(xlCellTypeLastCell).Row
'-------------------------------------------------------------------------
'fill "B" with results
For Each oCell In w2.Range("D2:D" & i)
'determinate row number for duplicated values
z = 1: While Dic2.exists(oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z): z = z + 1: Wend
'search
For Each key In Dic
If oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z = key Then
oCell.Offset(, -2).Value = Dic(key)
End If
Next
'correction of the dictionary in case
'when sheet "A" has less duplicates than sheet "B"
If oCell.Offset(, -2).Value = "" Then
Dic2.RemoveAll: z = 1
For Each key In Dic
If oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z = key Then
oCell.Offset(, -2).Value = Dic(key)
End If
Next
End If
'add to dictionary already passed results for
'the next duplicates testing
If Not Dic2.exists(oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z) Then
Dic2.Add oCell.Value & "_" & oCell.Offset(, 3).Value & "_" & z, ""
End If
Next
顺便说一下,当我测试你的代码时:
Set w1 = Workbooks("book1.xlsm").Worksheets(1)
它给了我一个错误。应该是这样吗?和w2相同
Set w1 = Workbooks.open(FULL_PATH_TO_WORKBOOK).Worksheets(1)
可以通过
获得FULL_PATH_TO_WORKBOOKThisworkbook.path & Application.PathSeparator & "book1.xlsm"
如果你把宏放在book1
中答案 1 :(得分:0)
如果新代码不是强制性的,您可以重新运行此Sub,这次比较PO编号,然后删除那些比较不合适的行。