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
위의 코드를 사용하여 아래 언급된 시트에서 여러 단어("열 A 시트1"의 단어를 "열 B 시트 2"의 단어로)를 찾아서 바꿉니다.
https://docs.google.com/spreadsheets/d/15TRLccDr_EAR8s78u-WGSkGpAecBf42_lhRkjCev_WE/edit?usp=sharing
그러나 다른 데이터에 대해 다른 시트(아래 언급된 대로)에 이를 적용하면 코드가 실패합니다. 즉, sheet1에 왜곡된 단어가 표시됩니다.
https://docs.google.com/spreadsheets/d/14ba9pQDjMPWJd4YFpGffhtVcHxml0LdUUVQ0prrOEUY/edit?usp=sharing
'A열 시트1'의 단어를 'B열 시트2'의 단어로 바꿀 수 있도록 도와주세요.
참고: 위 링크는 Google 스프레드시트에 대해 제공되었지만 Excel 2007 시트에 문제가 있습니다.
나는 VBA에 능숙하지 않기 때문에 전체 수정된 코드를 제공하여 도움을 요청합니다.
답변1
나는 당신이 원하는 것이 한 번만 교체하고 교체가 이루어지면 추가 규칙을 중지하는 것이라고 가정합니다. 두 번째 시트를 예로 들면, 12행 "그러나"는 "그러나"로 번역되어야 하며 "그러나"가 "hoyouever"로 번역되지 않도록 추가 규칙을 중지해야 합니다(규칙 #17은 "우리"를 "당신"으로 번역함). ).
해결 방법은 먼저 모든 것을 중간 기호로 변환하고 두 번째 라운드에서는 중간 기호에서 원하는 대체 기호로 변환하는 것입니다. 아래와 같이 코드를 약간 수정하면 작동합니다.
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