excel - 在一列中查找并替换为 2 列中的数据
问题描述
我想用“列表”表中的相应条目替换“数据”表中的 A 和 B 列。
我有一张在datasheet的 B列中列出的数百个名称的工作表。
我想用listsheet中列出的名称替换这些名称,它由三列组成:
name
, ID
,full name
两张纸上的名字是一样的。在下面的代码中,我将name替换为full name,但我还想添加 ID。
Sub myReplace()
Dim myDataSheet As Worksheet
Dim myReplaceSheet As Worksheet
Dim myLastRow As Long
Dim myRow As Long
Dim myFind As String
Dim myReplace As String
' Specify name of Data sheet
Set myDataSheet = Sheets("Wedstr")
' Specify name of Sheet with list of replacements
Set myReplaceSheet = Sheets("List")
' Assuming list of replacement start in column A on row 2, find last entry in list
myLastRow = myReplaceSheet.Cells(Rows.Count, "B").End(xlUp).Row
Application.ScreenUpdating = False
' Loop through all list of replacments
For myRow = 2 To myLastRow
' Get find and replace values (from columns A and B)
myFind = myReplaceSheet.Cells(myRow, "A")
myReplace = myReplaceSheet.Cells(myRow, "B")
' Start at top of data sheet and do replacements
myDataSheet.Activate
Range("A1").Select
' Ignore errors that result from finding no matches
On Error Resume Next
' Do all replacements on column A of data sheet
With Application.ReplaceFormat.Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0
.PatternTintAndShade = 0
End With
Columns("B").Replace What:=myFind, Replacement:=myReplace, LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True
Columns("D").Replace What:=myFind, Replacement:=myReplace, LookAt:=xlWhole, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True
' Reset error checking
On Error GoTo 0
Next myRow
Application.ScreenUpdating = True
MsgBox "Replacements complete!"
End Sub
解决方案
这是我将如何做到这一点。
Dim myDataSheet As Worksheet
Dim myReplaceSheet As Worksheet
Dim myLastRow As Long
Dim myRow As Long
Dim namedict As Object
Dim namearr(1) As Variant
Dim name As String
Set namedict = CreateObject("Scripting.Dictionary")
Set myDataSheet = Sheets("Wedstr")
Set myReplaceSheet = Sheets("List")
myLastRow = myReplaceSheet.Cells(Rows.Count, "B").End(xlUp).Row
with myreplacesheet
For myRow = 2 To myLastRow
name = .cells(myrow, "A").value
if not namedict.exists(name) then 'Make sure it doesn't error out if duplicates exist
namearr(0) = .cells(myrow, "B").value
namearr(1) = .cells(myrow, "C").value
namedict.add name, namearr
end if
next
end with
with mydatasheet
mylastrow = .cells(rows.count, "A").end(xlup).row
for myrow = 2 to mylastrow
name = .cells(myrow, "A").value
if namedict.exists(name) then 'Make sure name is in dictionary
.cells(myrow, "A").value = namedict(name)(0)
.cells(myrow, "B").value = namedict(name)(1)
end if
next
end with
我不确定我的列是否正确,所以请注意。
如果您想保留当前设置,可以查看 Range.Resize 属性。以下是相关文档:https ://docs.microsoft.com/en-us/office/vba/api/excel.range.resize
推荐阅读
- amazon-web-services - 在 AWS Elastic Beanstalk 和 EKS 上部署了一个 laravel 应用程序 相同的数据库 RDS 为什么在 Elastic Beanstalk 中获得快速响应
- c# - 我在使用 C# 对 Dynamics 365 普通用户进行身份验证时遇到错误
- codenameone - 正在编辑的文本字段的重新布局会扰乱模拟器中的文本
- angular - 处理不同类型的数据
- java - 使用默认 ObjectMapper 后布尔 JSON 参数为 false
- ios - 如何使用swift在ios的滚动视图下滚动特定视图?
- sql - SQL Select 根据多个状态字段值识别设备状态
- node.js - 如何使用 Asar 打包应用程序但不包括 Electron
- c# - C# UWP - OnNavigatedTo 触发方法两次
- r - 如何在 r 中制作交叉表