题
我需要替换字符串,但到目前为止,我只设法替换单词。代码正在通过描述列,并且应该替换列表中的每个序列。 这个想法是使用缩写(新值)而不是完整的单词(旧值)。
每次有相邻(无空间)字,字符等,代码都会失败。怎么了?谢谢!!!
Sub ReplaceWords_BUT_Need_ToReplaceStrings()
Application.ScreenUpdating = False
'This macro works with separated words only. Eliminate hyphens etc before applying this macro.
Dim r As Range, A As Range
Dim s1 As Worksheet, s2 As Worksheet, LastRow As Integer, v As String, j As Integer,
oldv As String, newv As String
LastRow = Cells(Rows.count, "A").End(xlUp).row
Set s1 = Sheets("JE_data")
Set s2 = Sheets("ListToReplace")
Set A = s1.Range("J:J").Cells.SpecialCells(xlCellTypeConstants)
For Each r In A
v = r.Value
For j = 2 To LastRow
oldv = s2.Cells(j, 1).text
newv = s2.Cells(j, 2).text
.
'替换函数,因为您使用的是区分大小写。 '你确定所有发票单词都以大写字母开头吗? '如果不是,则使用v=替换(v,oldv,newv,1,-1,vbtextcompare)功能。好的。
v = Replace(v, oldv, newv, 1, -1, vbTextCompare)
'v = Replace(v, oldv, newv)
Next j
r.Value = Trim(v)
Next r
Application.ScreenUpdating = True
End Sub
.
示例:
listtoreplace
Left column (old) Right column (new)
----------------------------------------
Invoice inv
Salary sy
Credit cr
.
je_data
cr Note
cr note
INV/2712/14RF/0229/14
Invoice 1078 10
TECHNOQ01SI 2014 03288
.
(编辑器注意:我不知道该示例是否正确,因为它是在评论中编写的)
Credit
用cr
替换,预期生成的icotagcode将用Invoice
替换,但它不是
解决方案
替换函数,因为您使用的是区分大小写。您确定所有发票单词都以大写字母开头吗?如果没有,则使用
v = Replace(v, oldv, newv, 1, -1, vbTextCompare)
.
功能。
不隶属于 StackOverflow