【问题标题】:Multiple Replace in VBAVBA中的多次替换
【发布时间】:2021-03-02 19:06:58
【问题描述】:

如何替换工作表中的多个单词?

类似的词:Da...da、Do...do、Dos、De...de.. 等

如何在名为“Customers3”的电子表格中调整它?

Public Function MyProper(MyString As String, Optional exceptions As Variant)

Dim c As Variant
If IsMissing(exceptions) Then
    exceptions = Array("a", "as", "e", "o", "os", "da", "das", "de", "di", "do", "dos",  _
      "CPF", "RG", "E-Mail")
End If

MyString = Application.Proper(MyString)

For Each c In exceptions
    MyString = Replace(" " & MyString & " ", " " & c & " ", " " & LCase(c) & " ", , , vbTextCompare)
Next c

MyProper = MyString

End Sub

【问题讨论】:

  • 你是如何尝试使用这个功能的?
  • 我有几个包含客户数据、姓名、地址的电子表格,我使用了“ProperFunction”来正确处理,但粒子的第一个字母也是大写的,不应该是这样。 -对不起,我不太懂英语
  • 在 Excel 中使用它会发生什么?例如,=MyProper(A1)?
  • 我需要在每个电子表格中更改它,并且由于我仍在学习,我不知道如何将它插入代码中。你能告诉我怎么做吗?

标签: excel vba replaceall


【解决方案1】:

这里有一个问题:

MyString = Replace(" " & MyString & " ", " " & c & " ", " " & LCase(c) & " ", , , vbTextCompare)

每次通过循环时,您都会添加更多空格...

你还有End Sub而不是End Function

试试这个:

Public Function MyProper(MyString As String, Optional exceptions As Variant)

    Dim c As Variant
    If IsMissing(exceptions) Then
        exceptions = Array("a", "as", "e", "o", "os", "da", _
                           "das", "de", "di", "do", "dos", _
                           "CPF", "RG", "E-Mail")
    End If
    
    MyString = " " & Application.Proper(MyString) & " " 'in case exception at start/end
    
    For Each c In exceptions
        MyString = Replace(MyString, " " & c & " ", " " & LCase(c) & " ", , , vbTextCompare)
    Next c
    
    MyProper = Trim(MyString) 'remove any added spaces

End Function

【讨论】:

  • 好的,等我回家,我试试。但提前,非常感谢你。
  • 您好,我完全按照您发布的内容进行复制,但什么也没发生。我用的是最下面的,但是很“重”,执行时间长(有很多“替换”):.....>
  • Sub ProperCase() Dim rnge As range For Each rnge In Selection.SpecialCells(xlCellTypeConstants, xlTextValues).Cells rnge.Value = StrConv(rnge.Value, vbProperCase) rnge.Value = Replace(Replace(替换(替换(替换(替换(替换)(替换(替换(替换(替换(替换(rnge.Value,_“达”,“达”),“达斯”,“达斯”),“德”,“德” )、"Dos"、"dos")、"Do"、"do")、"A"、"a")、"O"、"o")、_"Os"、"os")、"Cpf ", "CPF"), "Rg", "RG"), "E_mail", "E_Mail"), "E", "e") Next rnge End Sub
  • 这对我来说是一个函数——它本身不会“做”任何事情。
【解决方案2】:

Proper葡萄牙语

Option Explicit

Function MyProper(ByVal MyString As String) As String

    Const ExceptionsList As String _
        = "a,as,e,o,os,da,das,de,di,do,dos,CPF,RG,E-Mail"
    Dim Exceptions() As String: Exceptions = Split(ExceptionsList, ",")
    
    Dim SubStrings() As String
    SubStrings = Split(Application.Proper(MyString), " ")
    
    Dim cIndex As Variant
    Dim n As Long
    For n = 0 To UBound(SubStrings)
        cIndex = Application.Match(SubStrings(n), Exceptions, 0)
        If IsNumeric(cIndex) Then
            SubStrings(n) = Exceptions(cIndex - 1)
        End If
    Next n
    
    MyProper = Join(SubStrings, " ")

End Function


Sub MyProperAllWorksheets()
    
    Dim wb As Workbook: Set wb = ThisWorkbook ' workbook containing this code
    Dim ws As Worksheet
    Dim rg As Range
    Dim Data As Variant
    Dim rCount As Long, cCount As Long
    Dim r As Long, c As Long
    
    For Each ws In wb.Worksheets
        Set rg = ws.UsedRange
        rCount = rg.Rows.Count
        cCount = rg.Columns.Count
        If rCount > 1 Or cCount > 1 Then
            Data = rg.Value
        Else
            ReDim Data(1 To 1, 1 To 1): Data(1, 1) = rg.Value
        End If
        For r = 1 To rCount
            For c = 1 To cCount
                On Error Resume Next
                Data(r, c) = MyProper(Data(r, c))
                On Error GoTo 0
            Next c
        Next r
        rg.Value = Data
    Next ws

End Sub

【讨论】:

  • 嗨。我完全按照您发送的方式复制和粘贴,但什么也没发生。该程序甚至没有想到。
  • 过程MyProperAllWorksheets 是为ThisWorkbook 编写的,即包含此代码的工作簿。如果您想将它用于另一个工作簿,您应该将ThisWorkbook 替换为ActiveWorkbook,或者更好的是,使用工作簿名称,例如Workbooks("Test.xlsx").
  • Muito obrigado! (非常感谢)。始终戴上口罩。上帝与你同在。
  • 完美,朋友!确定! Muito muito muito obrigado。 Deus cuidará de vc sempre! (完美,朋友!成功了!非常感谢。上帝会一直照顾你的!)。
猜你喜欢
  • 2023-02-05
  • 2019-04-18
  • 2015-05-19
  • 1970-01-01
  • 2021-01-02
  • 2021-07-21
  • 1970-01-01
  • 2011-10-24
  • 1970-01-01
相关资源
最近更新 更多