[英]Excel Find & Replace Macro
I have a worksheet which imports all of my orders, however when creating labels, I only have a limited amount of space for the title. 我有一个导入所有订单的工作表,但是在创建标签时,标题的空间有限。 I'm using a Find & Replace Macro in Excel which looks in my current active imported worksheet, and replaces with text from another worksheet which I use as a table with 2 columns, Column A is what the title is when imported and Column B is what I want to change it to.
我在Excel中使用“查找并替换宏”,该宏在我当前活动的导入工作表中查找,并替换为另一个工作表中的文本,该工作表用作具有2列的表,列A是导入时的标题,列B是我想要将其更改为。 This script works perfectly fine except it doesn't find columns that have a different beginning.
该脚本工作得很好,除了它找不到开头不同的列。 For example:
例如:
Imported Worksheet: 导入的工作表:
Entry 1: BANANAS 参赛作品1:香蕉
Entry 2: 30 X BANANAS 参赛作品2:30 X BANANAS
Table: 表:
Column A: BANANAS 专栏A:香蕉
Column B: Yellow Bananas B栏:黄香蕉
//Script Runs// //脚本运行//
Output: Imported Worksheet: 输出:导入的工作表:
Entry 1: Yellow Bananas 参赛作品1:黄香蕉
Entry 2: 30 X BANANAS 参赛作品2:30 X BANANAS
As you can see in the example above, the "30 X BANANAS" entry does not change to "30 X Yellow Bananas", as I would want it to. 正如您在上面的示例中看到的那样,正如我希望的那样,“ 30 X BANANAS”条目不会更改为“ 30 X Yellow Bananas”。 I'm guessing I need to add a wildcard line of code to my script below, but I'm not sure how to incorporate it?
我猜我需要在下面的脚本中添加一个通配符代码行,但是我不确定如何将其合并?
Sub FindReplace()
Dim s As String
Dim cell As Range
For Each cell In Range("H3:H5000").Cells
If cell <> "" Then
ans = Application.VLookup(cell, Sheets("Script").Range("A1:B1000"), 2, 0)
If Not IsError(ans) Then cell = ans
End If
Next cell
End Sub
If you use the Range.Replace function rather than the VLookup function, you will get the result you are looking for. 如果使用Range.Replace函数而不是VLookup函数,则将获得所需的结果。
Before 之前
After 后
Sub FindNReplace()
Dim InputRng As Range, ReplaceRng As Range
'Set Range Values
Set InputRng = Range("A1:A5")
Set ReplaceRng = Sheets("Script").Range("A1:B100")
'Spin through replacement range and perform replace
For Each Rng In ReplaceRng.Columns(1).Cells
InputRng.Replace what:=Rng.Value, replacement:=Rng.Offset(0, 1).Value
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.