Excel 查找单个匹配值并将其替换为多个新值
Excel Find and replace a single match value with multiple new values
我有 2 个 excel sheet,我想在其中查找和替换值,但是我想让多个替换值代替一个匹配值。
Sheet 1: Sheet 2:
Match Value Match Value New Value
28045000 28045000 28051560
39162010 28045000 28056549
39269000 39162010 39596000
sheet1 中的所有匹配值都是唯一的,而sheet2 中的匹配值可能有重复,因为它们对应于多个新值。因此,如果 sheet 1 和 sheet 2 中的匹配值相同,那么我想用匹配项对应的所有新值替换 sheet 1 中的匹配值价值。 Sheet 1 替换完成后应如下所示:
Sheet 1:
Match Value
28051560
28056549
39596000
39269000
所以我们可以看到,28045000 在 2 个单独的单元格中被 2 个值替换,28051560 和 28056549,而 39162010 被 39596000 替换,而 39269000 在 sheet 2 中没有匹配值,保持不变。
我通常会手动执行此操作,但大约有 30,000 行数据,其中一些有超过 10 个值与单个匹配值匹配。我有以下代码,但是,这并没有用所有新值正确替换匹配值。有没有办法让 Excel 搜索两个 sheet 的整个范围并自动进行适当的更改?
Sub multiFindNReplace()
Dim myList, myRange
Set myList = Sheets("sheet 1").Range("A1:A5000")
Set myRange = Sheets("sheet2").Range("A1:A5000")
For Each cel In myList.Columns(1).Cells
myRange.Replace what:=cel.Value, replacement:=cel.Offset(0, 1).Value
Next cel
End Sub
我会这样做:
宏只是循环遍历第一个 sheet 并将其与第二个 sheet 进行比较。如果匹配,则替换第一个的值,加上 c+1 继续查找。因为原始值随后被替换,原始值存储在 d 中,如果它找到第二个匹配它不会因为 c+1 而替换它,它会转到 else 子句,插入一行并将值放入新行。像这样循环遍历 sheet1.
上的整个列
PS:希望你能理解,我没有那么多时间,稍后会编辑以提高可读性。
更新:
所以我们再来一次,我添加了 maxrow 计数器并对其进行了过度注释以便于理解。
更新二:
现在使用 While-Loop 因为 for-loops 不会重新定义限制更改
Sub CompareLoop()
'Iterator Worksheet 1, is the counter for the ws1 column
Dim iWS1 As Integer
'Iterator Worksheet 2, is the counter for the ws1 column
Dim iWS2 As Integer
'Switch New Row, is the switch if the next value need a new row
Dim sNR As Integer
'Maximal Row Count, need to be extend when new rows are added
Dim MaxRows As Integer
'valueHolder, is the holder for the orginal value, the orginal value might be replaced on the sheet
Dim valueHolder As Long
'Worksheet1
Dim ws1 As Worksheet
'Worlsheet2
Dim ws2 As Worksheet
Set ws1 = ActiveWorkbook.Worksheets("table1")
Set ws2 = ActiveWorkbook.Worksheets("table2")
'Set iWS1 to the first row
iWS1 = 1
'Get MaxRows
MaxRows = ws1.Cells(Rows.Count, 1).End(xlUp).Row
'Loop through the Rows on WS1 setting switch to 0 and store the value from the ws1 row in the holder
While iWS1 <= MaxRows
sNR = 0
valueHolder = ws1.Cells(iWS1, 1).Value
'Loop through the Rows on WS2, searching for a value that match with the value from ws1
For iWS2 = 1 To ws2.Cells(Rows.Count, 1).End(xlUp).Row
'When it matches, then look if there was already a match with the value, if not replace it on the ws1 and increase the sNr to 1
If valueHolder = ws2.Cells(iWS2, 1).Value Then
If (sNR < 1) Then
ws1.Cells(iWS1, 1).Value = ws2.Cells(iWS2, 2).Value
sNR = sNR + 1
'When the sNR is already > 0, increase the Iterator for the ws1 that he will point on the new line
'increase the maxrows because we got one more soon, finally insert the new row and store the value from ws2 in it
Else
iWS1 = iWS1 + 1
MaxRows = MaxRows + 1
Range(ws1.Cells(iWS1, 1), ws1.Cells(iWS1, 1)).EntireRow.Insert
ws1.Cells(iWS1, 1).Value = ws2.Cells(iWS2, 2)
End If
End If
Next iWS2
iWS1 = iWS1 + 1
Wend
End Sub
假设从 A 开始的列是连续的并且在 Sheet 1、B2 中被标记并向下复制以适应:
=IF(ISERROR(MATCH(A2,'Sheet 2'!A:A,0)),A2,"")
复制包含 Sheet 1 B 列中所有值的范围并选择性粘贴,低于 Sheet 2 B 列中最后一个条目的值。
复制Sheet 2 B列到Sheet 1的A1中,过滤去除A列中的空白。删除Sheet 1 B列。
我有 2 个 excel sheet,我想在其中查找和替换值,但是我想让多个替换值代替一个匹配值。
Sheet 1: Sheet 2:
Match Value Match Value New Value
28045000 28045000 28051560
39162010 28045000 28056549
39269000 39162010 39596000
sheet1 中的所有匹配值都是唯一的,而sheet2 中的匹配值可能有重复,因为它们对应于多个新值。因此,如果 sheet 1 和 sheet 2 中的匹配值相同,那么我想用匹配项对应的所有新值替换 sheet 1 中的匹配值价值。 Sheet 1 替换完成后应如下所示:
Sheet 1:
Match Value
28051560
28056549
39596000
39269000
所以我们可以看到,28045000 在 2 个单独的单元格中被 2 个值替换,28051560 和 28056549,而 39162010 被 39596000 替换,而 39269000 在 sheet 2 中没有匹配值,保持不变。
我通常会手动执行此操作,但大约有 30,000 行数据,其中一些有超过 10 个值与单个匹配值匹配。我有以下代码,但是,这并没有用所有新值正确替换匹配值。有没有办法让 Excel 搜索两个 sheet 的整个范围并自动进行适当的更改?
Sub multiFindNReplace()
Dim myList, myRange
Set myList = Sheets("sheet 1").Range("A1:A5000")
Set myRange = Sheets("sheet2").Range("A1:A5000")
For Each cel In myList.Columns(1).Cells
myRange.Replace what:=cel.Value, replacement:=cel.Offset(0, 1).Value
Next cel
End Sub
我会这样做:
宏只是循环遍历第一个 sheet 并将其与第二个 sheet 进行比较。如果匹配,则替换第一个的值,加上 c+1 继续查找。因为原始值随后被替换,原始值存储在 d 中,如果它找到第二个匹配它不会因为 c+1 而替换它,它会转到 else 子句,插入一行并将值放入新行。像这样循环遍历 sheet1.
上的整个列PS:希望你能理解,我没有那么多时间,稍后会编辑以提高可读性。
更新:
所以我们再来一次,我添加了 maxrow 计数器并对其进行了过度注释以便于理解。
更新二:
现在使用 While-Loop 因为 for-loops 不会重新定义限制更改
Sub CompareLoop()
'Iterator Worksheet 1, is the counter for the ws1 column
Dim iWS1 As Integer
'Iterator Worksheet 2, is the counter for the ws1 column
Dim iWS2 As Integer
'Switch New Row, is the switch if the next value need a new row
Dim sNR As Integer
'Maximal Row Count, need to be extend when new rows are added
Dim MaxRows As Integer
'valueHolder, is the holder for the orginal value, the orginal value might be replaced on the sheet
Dim valueHolder As Long
'Worksheet1
Dim ws1 As Worksheet
'Worlsheet2
Dim ws2 As Worksheet
Set ws1 = ActiveWorkbook.Worksheets("table1")
Set ws2 = ActiveWorkbook.Worksheets("table2")
'Set iWS1 to the first row
iWS1 = 1
'Get MaxRows
MaxRows = ws1.Cells(Rows.Count, 1).End(xlUp).Row
'Loop through the Rows on WS1 setting switch to 0 and store the value from the ws1 row in the holder
While iWS1 <= MaxRows
sNR = 0
valueHolder = ws1.Cells(iWS1, 1).Value
'Loop through the Rows on WS2, searching for a value that match with the value from ws1
For iWS2 = 1 To ws2.Cells(Rows.Count, 1).End(xlUp).Row
'When it matches, then look if there was already a match with the value, if not replace it on the ws1 and increase the sNr to 1
If valueHolder = ws2.Cells(iWS2, 1).Value Then
If (sNR < 1) Then
ws1.Cells(iWS1, 1).Value = ws2.Cells(iWS2, 2).Value
sNR = sNR + 1
'When the sNR is already > 0, increase the Iterator for the ws1 that he will point on the new line
'increase the maxrows because we got one more soon, finally insert the new row and store the value from ws2 in it
Else
iWS1 = iWS1 + 1
MaxRows = MaxRows + 1
Range(ws1.Cells(iWS1, 1), ws1.Cells(iWS1, 1)).EntireRow.Insert
ws1.Cells(iWS1, 1).Value = ws2.Cells(iWS2, 2)
End If
End If
Next iWS2
iWS1 = iWS1 + 1
Wend
End Sub
假设从 A 开始的列是连续的并且在 Sheet 1、B2 中被标记并向下复制以适应:
=IF(ISERROR(MATCH(A2,'Sheet 2'!A:A,0)),A2,"")
复制包含 Sheet 1 B 列中所有值的范围并选择性粘贴,低于 Sheet 2 B 列中最后一个条目的值。
复制Sheet 2 B列到Sheet 1的A1中,过滤去除A列中的空白。删除Sheet 1 B列。