简体   繁体   English

批量查找并替换列单元格中的相同值

[英]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.

相关问题 在列中查找值并选择同一行中的单元格 - find values in a column and select the cells in the same row 在列中,找到所有带有相同文本的单元格。 然后将同一行但不同列中的所有值相加 - In a column, find all cells with same text inside. Then sum all the values in the same rows but different column 查找一列中具有相同值的单元格,并从同一行的不同列中返回值 - Find cells with same value within one column and return values from separate column of same row 使用 VBA 在连续单元格中查找和替换值 - Find and Replace Values in Consecutive Cells Using VBA 在A列中找到多个单元格值,并在同一行的D列中替换为变量值 - Find multiple cell values in column A and replace with variable values in column D of same row 如果两列中的值相同,则合并熊猫中的单元格 - Merge cells in pandas if values in two column is same 根据相似的值将单元格对齐到同一列 - Aligning cells into the same column based on similar values 取消合并和填充值-如果单元格合并到同一列 - Unmerge and fill values - if the cells are merged to the same column Pandas 合并具有相同值的第一列中的单元格 - Pandas Merge Cells in First Column with Same Values 在 TXT 文件中查找值并替换为 Excel 单元格中的值 - Find values in TXT file and replace with values from Excel cells
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM