vba 在其他工作表上使用 vlookup 查找和替换值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22835686/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find and replace value with a vlookup on other sheet
提问by Gerry
I have multiple cells containing text data and would like to replace some data with data from another sheet.
我有多个包含文本数据的单元格,并想用另一张工作表中的数据替换一些数据。
Example
例子
Sheet 1
第 1 页
A1= "aa*a"
B1= "b]bb"
C1= "cae*"
A1="aa*a"
B1="b]bb"
C1="cae*"
Sheet 2
第 2 页
A1="q" B1="Quote"
A2="e" B2="Example"
A1="q" B1="报价"
A2="e" B2="示例"
Result
结果
A1
stays the same, B1
stays the same and C1
changes to caExample*
A1
保持不变,B1
保持不变并C1
更改为caExample*
I guess I need an VBA code for a find and replace with an VLookUp
.
我想我需要一个 VBA 代码来查找并替换为VLookUp
.
Can anybody help me out?
有人可以帮我吗?
采纳答案by Gary's Student
Give this a try:
试试这个:
Sub PolyChange()
Dim s1 As Worksheet, s2 As Worksheet
Set s1 = Sheets("Sheet1")
Set s2 = Sheets("Sheet2")
Dim I As Long, J As Long, v1 As String
Dim N1 As Long, N2 As Long, v2 As String, v3 As String
N1 = s1.Cells(Rows.Count, "A").End(xlUp).Row
N2 = s2.Cells(Rows.Count, "A").End(xlUp).Row
For I = 1 To N1
v1 = s1.Cells(I, "A")
For J = 1 To N2
v2 = s2.Cells(J, "A")
v3 = s2.Cells(J, "B")
v1 = Replace(v1, v2, v3)
Next J
s1.Cells(I, "A").Value = v1
Next I
End Sub