[英]How do I make a Replace function in Excel only run through the first occurrence of a word in another Excel workbook?
I want to run through replacement words in one worksheet and replace the first occurrence of those words in another workbook. 我想在一个工作表中运行替换单词,并替换另一个工作簿中第一次出现的单词。 Column A gives a word and Column B gives a value used to replace occurrences of words in Column A.
A列给出一个单词,B列给出一个值,用于替换A列中出现的单词。
The code I am using replaces all occurrences of the words in Column A to Column B. 我使用的代码替换了A列到B列中出现的所有单词。
Sub Sample2()
Dim NameListWB As Workbook, thisWb As Workbook
Dim NameListWS As Worksheet, thisWs As Worksheet
Dim i As Long, lRow As Long
Set thisWb = ThisWorkbook
Set thisWs = thisWb.Sheets("Sheet1")
Set NameListWB = Workbooks.Open("C:document.xlsx")
Set NameListWS = NameListWB.Worksheets("Sheet2")
With thisWs
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
For i = 1 To lRow
NameListWS.Columns(1).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(2).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(3).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(4).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(5).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Text, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(6).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(7).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
For i = 1 To lRow
NameListWS.Columns(8).Replace What:=.Range("A" & i).Value, _
Replacement:=.Range("B" & i).Value, _
SearchOrder:=xlByColumns, _
MatchCase:=False
Next i
End With
End Sub
Sub Sample2()
Dim NameListWB As Workbook, thisWb As Workbook
Dim NameListWS As Worksheet, thisWs As Worksheet
Dim i As Long, lRow As Long, columnNum As Long
Dim maxColumnToRepalce As Integer
Dim findRange As Range
Set thisWb = ThisWorkbook
Set thisWs = thisWb.Sheets("Sheet1")
Set NameListWB = Workbooks.Open("C:document.xlsx")
Set NameListWS = NameListWB.Worksheets("Sheet2")
maxColumnToRepalce = 8
With thisWs
lRow = .Range("A" & .Rows.Count).End(xlUp).Row
For columnNum = 1 To maxColumnToRepalce
For i = 1 To lRow
Set findRange = NameListWS.Columns(columnNum).Find(What:=.Range("A" & i).Value, _
SearchOrder:=xlByColumns, _
after:=.Cells(Columns(columnNum).Rows.Count, columnNum), _
MatchCase:=False)
If Not findRange Is Nothing Then
findRange.Value = .Range("B" & i)
End If
Next i
Next columnNum
End With
End Sub
Use Find method to find the first occurrence then do the replacement. 使用Find方法查找第一个匹配项,然后执行替换。 By The Way, you can simplify the code by adding another loop.
通过The Way,您可以通过添加另一个循环来简化代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.