在Excel 2007中查找并替换列中的多个单词


1
Sub xLator2()
Dim s1 As Worksheet, s2 As Worksheet
Dim N As Long, i As Long
Dim from(), too()
Set s1 = Sheets("Sheet1") '   contains the data
Set s2 = Sheets("Sheet2") '   contains the translation table

s2.Activate

N = Cells(Rows.Count, 1).End(xlUp).Row
ReDim from(1 To N)
ReDim too(1 To N)
For i = 1 To N
    from(i) = Cells(i, 1).Value
    too(i) = Cells(i, 2).Value
Next i

s1.Activate

For i = LBound(from) To UBound(from)
    Cells.Replace What:=from(i), Replacement:=too(i)
Next i
End Sub

我正在使用上面的代码在下面提到的工作表中查找和替换多个单词(“ Column A Sheet1”中的单词与“ Column B Sheet 2”中的单词):

https://docs.google.com/spreadsheets/d/15TRLccDr_EAR8s78u-WGSkGpAecBf42_lhRkjCev_WE/edit?usp=sharing

但是,当我在另一个工作表(如下所述)中将其应用于另一个数据时,代码将失败,即我在工作表1中得到了失真的单词:

https://docs.google.com/spreadsheets/d/14ba9pQDjMPWJd4YFpGffhtVcHxml0LdUUVQ0prrOEUY/edit?usp=sharing

请帮帮我,以便我可以将“ A列Sheet1”中的单词替换为“ B列Sheet2”中的单词

注意:上面的链接已经给出了Google电子表格,但是我在Excel 2007工作表中遇到了问题。

我要求您通过提供完整的修订代码来帮助我,因为我在VBA中表现不佳

Answers:


0

我假设您想要的是只进行一次替换,一旦完成替换就停止进一步的规则。以您的第二张纸为例,第12行的“ but”应翻译为“ however”,并停止其他规则,以使“ however”不会翻译为“ hoyouever”(规则17将“ we”翻译为“ you”) )。

解决方法是先将所有内容转换为某个中间符号,然后在第二轮中将中间符号转换为所需的替换符号。如下修改代码即可:

Sub xLator2()
Dim s1 As Worksheet, s2 As Worksheet
Dim N As Long, i As Long
Dim from(), too()
Set s1 = Sheets("Sheet1") '   contains the data
Set s2 = Sheets("Sheet2") '   contains the translation table

s2.Activate

N = Cells(Rows.Count, 1).End(xlUp).Row
ReDim from(1 To N)
ReDim too(1 To N)
For i = 1 To N
    from(i) = Cells(i, 1).Value
    too(i) = Cells(i, 2).Value
Next i

s1.Activate

' -------------- Modification starts here --------------------------
' Replace from from(i) to __MYREPLACEMENTi__  (where i is the counter)
For i = LBound(from) To UBound(from)
    Cells.Replace What:=from(i), Replacement:="__MYREPLACEMENT" + Str(i) + "__"
Next i
' Replace from __MYREPLACEMENTi__ to too(i)  (where i is the counter)
For i = LBound(from) To UBound(from)
    Cells.Replace What:="__MYREPLACEMENT" + Str(i) + "__", Replacement:=too(i)
Next i
' -------------- Modification ends here --------------------------
End Sub

亲爱的肯尼斯,上面的代码可以使用到docs.google.com/spreadsheets/d/…中提到的20行, 但是仍然不能使用:docs.google.com/spreadsheets/d/…,其中总共包含101行。您能否提供可以一次用于数百或数千行的内容。
Sidharth 2014年

当您回答时,请提供完整的代码,以便我可以复制相同的代码。
Sidharth 2014年

等待回复
Sidharth 2014年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.