VBA查找并替换

时间:2018-11-22 14:33:01

标签: excel vba excel-vba replace find

我正在使用下面的代码,在excel上进行查找和替换:

Sub abbrev()
Dim abvtab() As Variant
Dim ltsheet As Worksheet
Dim datasheet As Worksheet
Dim lt As Range
Dim i As Long
Set ltsheet = Sheets("sheet2")
' REFERENCE TO SINGLE WORKSHEET
'  Set datasheet = Sheets("ACTIVE_DIRECTORY_User")
Set lt = ltsheet.Range("A2", ltsheet.Range("B2").End(xlDown))
abvtab = lt
For Each datasheet In Worksheets
If datasheet.Name <> ltsheet.Name Then
For i = 1 To UBound(abvtab)
datasheet.Cells.Replace What:=abvtab(i, 1), Replacement:=abvtab(i, 2), 
LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True, SearchFormat:=False, _
ReplaceFormat:=False
Next i
End If
Next datasheet
End Sub

但是,似乎无法找到部分单词,例如,如果我要替换单词本,则代码将忽略单词书架

是否有一种方法可以设置上面的代码以匹配确切的单词,而不管它是句子的一部分还是例如带有空格?

2 个答案:

答案 0 :(得分:1)

最后我不得不放弃vba,取而代之的是我使用了C#Regex.Replace,通过excel查找和替换有很多问题,因此下面的解决方案似乎很好用,尽管下面的解决方案我也不得不放弃了csv是我需要使用的错误文件格式,我将改用mysql,为了防止有人从中受益并使用它来进行大规模查找和替换,我将这两种代码粘贴到下面的两种代码中

onclick

答案 1 :(得分:0)

怎么样?

def customloss(y_true,y_pred):
    return K.binary_crossentropy(y_true,y_pred) + weight_layer
m2.compile(optimizer='adam',loss=customloss,...)

注意通配符:Sub Multi_FindReplace() Dim sht As Worksheet Dim fndList As Variant Dim rplcList As Variant Dim x As Long fndList = Array("*" & "Ca" & "*", "United States", "Mexico") rplcList = Array("CAN", "USA", "MEX") 'Loop through each item in Array lists For x = LBound(fndList) To UBound(fndList) 'Loop through each worksheet in ActiveWorkbook For Each sht In ActiveWorkbook.Worksheets sht.Cells.Replace What:=fndList(x), Replacement:=rplcList(x), _ LookAt:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, _ SearchFormat:=False, ReplaceFormat:=False Next sht Next x End Sub

之前:

enter image description here

之后:

enter image description here