[英]Bulk find and replace same values of in cells of a column
I am trying to create an Excel VBA function to search values of column A and find same cells in column H, and replace these cells in B2:F6 with values of J2:N4.我正在尝试创建一个 Excel VBA 函数来搜索 A 列的值并在 H 列中找到相同的单元格,并将 B2:F6 中的这些单元格替换为 J2:N4 的值。
My Input File:我的输入文件:
Desired Output:期望的输出:
I have tried the following VBA code but it doesn't work.我尝试了以下 VBA 代码,但它不起作用。 it finds and replace the values of column Replace1 and ignores Replace 2,3,... .
它查找并替换列 Replace1 的值并忽略 Replace 2,3,... 。
Sub MultiFindNReplace()
'Update 20140722
Dim Rng As Range
Dim InputRng As Range, ReplaceRng As Range
xTitleId = "KutoolsforExcel"
Set InputRng = Application.Selection
Set InputRng = Application.InputBox("Original Range ", xTitleId, InputRng.Address, Type:=8)
Set ReplaceRng = Application.InputBox("Replace Range :", xTitleId, Type:=8)
Application.ScreenUpdating = False
For Each Rng In ReplaceRng.Columns(1).Cells
InputRng.Replace what:=Rng.Value, replacement:=Rng.Offset(0, 1).Value
Next
Application.ScreenUpdating = True
End Sub
Looks like both datasets got same headers so you can benefit from that.看起来两个数据集都有相同的标头,因此您可以从中受益。 If the headers are always the same and same sorting, just copy whole row:
如果标题始终相同且排序相同,则只需复制整行:
Sub test()
'if headers of both datasets are always the same and sorted the same way, just copy whole row
Dim rngDestiny As Range
Dim rngSource As Range
Dim rngFind As Range
Dim rng As Range
Dim i As Long
Dim RowN As Long
Dim LR As Long
Set rngSource = Range("I2:M4")
Set rngFind = Range("H2:H4")
Set rngDestiny = Range("B2:F6")
LR = Range("A" & Rows.Count).End(xlUp).Row 'last non-blank cell in column f-name
For i = 2 To LR Step 1
With Application.WorksheetFunction
'check if the value of f-name exists in column FIND
If .CountIf(rngFind, Range("A" & i).Value) > 0 Then
'there is a match, get row number and copy
RowN = .Match(Range("A" & i).Value, rngFind, 0)
rngSource.Rows(RowN).Copy rngDestiny.Rows(i - 1) 'minus 1 because our first row of data starts with i=2!!!
End If
End With
Next i
Set rngSource = Nothing
Set rngFind = Nothing
Set rngDestiny = Nothing
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.