我有一个vba代码,可以批量替换。

我想(通过VBA或公式)将每个单元格的第一次出现的“%”替换为“(”,将第二次出现的“%”替换为“)”。

我的数据在B列中。

这是我的VBA代码:

Sub Multi_FindReplace()

Dim sht As Worksheet
Dim fndList As Variant
Dim rplcList As Variant
Dim x As Long

fndList = Array("_", "VOSTFR.mp4", "VF.mp4")
rplcList = Array(" ", "VOSTFR", "VF")

'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


谢谢 ! :)

最佳答案

Cell C1中输入以下任一公式:

=SUBSTITUTE(SUBSTITUTE(B1,"%","(",1),"%",")",1)


要么

=REPLACE(REPLACE(B1,FIND("%",B1),1,"("),FIND("%",REPLACE(B1,FIND("%",B1),1,"(")),1,")")


根据需要拖动/复制。参见图片以供参考。

excel - 用不同的字符替换第一次出现和第二次出现-LMLPHP

如果少于两个%,则公式将引发错误。因此,为避免错误,在公式中使用IFERROR作为

=IFERROR(REPLACE(REPLACE(B1,FIND("%",B1),1,"("),FIND("%",REPLACE(B1,FIND("%",B1),1,"(")),1,")"),"")

关于excel - 用不同的字符替换第一次出现和第二次出现,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44326537/

10-10 18:53