宏观改变&运算符连接公式= = CONCATENATE(a,b,c)

时间:2016-03-28 04:22:17

标签: excel vba excel-vba

我想创建一个函数,它将使用&更改具有连接元素的单元格。使用CONCATENATE()函数将符号转换为一个符号。因此,举例来说,我希望将function ="There are "&7&" cats"的单元格替换为=CONCATENATE("There are ",7," cats")

我有一个我想写的代码的骨架,但是我实际上无法正常工作。我认为这将是一个有趣的项目,让我尝试VBA,但我很快意识到,即使在尝试了世界上所有的Google-fu之后,我仍然试图编写这段代码。

到目前为止,我有以下内容:

Function fixConcatenate()
'For each cell in range, replace function elements to swap from & operator concatenation to CONCATENATE() function

    For Each c In ActiveCell.CurrentRegion.Cells
    'Insert "=CONCATENATE(" by replacing existing "="
    Range.Replace("=","=CONCATENATE(")

        'If "&" exists inside string, ignore it
        'Else replace "&" with ","
        'End function in cell with ")"

    Next

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:1)

我会使用正则表达式用“CONCATENATE”公式替换所有的&符号:

Sub UsageExample()

  ReplaceAmpersandByConcat ActiveCell.CurrentRegion

End Sub

Sub ReplaceAmpersandByConcat(target As Range)
  Dim re As Object, cl As Range, str As String

  ' create the regex object
  Set re = CreateObject("VBScript.RegExp")
  re.pattern = "(""[^""]*""|[^&]+)(\s*&\s*)"
  re.Global = True

  ' replace each ampersand concatenation with a formula
  For Each cl In target.Cells
    str = cl.formula
    ' if starts with "=" and contains "&" and not "=CONCATENATE"
    If InStrRev(str, "=", 1) = 1 And InStr(str, "&") > 0 And InStr(str, "=CONCATENATE") = 0 Then
      ' replace the ampersand characters
      cl.formula = "=CONCATENATE(" & re.replace(Mid$(str, 2), "$1,") & ")"
    End If
  Next

End Sub

请注意,它不会转换文本中的&符号(例如:A1&“D& B”),它将跳过已转换的单元格。

答案 1 :(得分:0)

您应该能够使用以下代码来使用以下代码来实现目标。这里的关键是访问公式本身,该公式存储在for循环中构造的“c”变量中。其次,您需要使用串联替换该公式的组件。第三,我通过添加特定短语[引用开始]和[引用结束]以指示“&”来绕过“if语句”。符号应该被替换或实际上是一个合法的字符串组件。

Function fixConcatenate()
'For each cell in range, replace function elements to swap from & operator concatenation to CONCATENATE() function

For Each c In ActiveCell.CurrentRegion.Cells
string_Update = c.Formula
Count = Len(string_Update) - Len(Replace(string_Update, """", ""))
For i = 1 To Count
If i Mod 2 = 0 Then
string_Update = Replace(string_Update, """", "[Quote End]", , i)
Else
string_Update = Replace(string_Update, """", "[Quote Start]", , i)
End If
Next i

string_Update = Replace(string_Update, "=", "=CONCATENATE(")
string_Update = Replace(string_Update, "[Quote End]&", """,")
string_Update = Replace(string_Update, "&[Quote Start]", ",""")
string_Update = Replace(string_Update, "[Quote Start]", ",""")
string_Update = Replace(string_Update, "[Quote End]", """,")
string_Update = string_Update + ")"
c.Formula = string_Update
Next
End Function

答案 2 :(得分:-1)

根据您的要求尝试以下代码

Sub test1()
Dim wb As Workbook
Dim ws As Worksheet
Dim Rng As Range
Dim t1 As Variant

Set wb = ThisWorkbook
Set ws = wb.Worksheets("sheet1")
Set Rng = ws.UsedRange



'Looping each cell

For Each c In Rng

    t1 = c.Formula
    Length = Len(c)

    For i = 1 To Length
       ' Changing to concatenate formula
        'Debug.Print Mid(t1, i, 1)
        If Mid(t1, i, 1) = "&" Then
            If Mid(t1, i - 1, 1) = """" Then
                t2 = Replace(t1, "&", ",")
                t3 = Replace(t2, "=", "=concatenate(")
                t3 = t3 & ")"
                c.Formula = t3
            End If
        End If
    Next

Next

End Function