2016-03-28 80 views
0

我想要创建一个函数,将使用CONCATENATE()函数将具有使用&符号级联元素的单元格更改为一个。所以,作为一个例子,我想要一个function ="There are "&7&" cats"的单元替换为=CONCATENATE("There are ",7," cats")宏要更改&运算符连接公式= CONCATENATE(a,b,c)

我有我想写的代码的骨架,但我实际上使它工作的麻烦。我认为这将是一个有趣的项目,让我尝试使用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 

任何帮助将不胜感激!

回答

0

您应该能够使用以下代码来实现您的目标。这里的关键是访问存储在每个循环中构造的“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 
+1

您在下降这里兔子洞。考虑这对'=“A”“”&“A”是什么,它会返回'A“A'(导致'= CONCATENATE(,”A“,”,,“,”A,“)'' A,A,') –

+0

在这里你是绝对正确的,这需要解决,如果报价是在这个帐户。 – TsTeaTime

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“),它将跳过已经转换的单元格。

+0

作品像一个魅力!我只是希望我知道从哪里开始写这样的事情。我不能说我以前听说过任何一个正则表达式,但是我肯定会阅读它的,谢谢! –

-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 
+0

嗯,当我尝试使用该函数时,出现了VALUE错误。我真的很感兴趣,看看这是如何工作作为一个函数与子,所以感谢您的意见!我一定会打破这个试图从中学习。 –

+0

我不知道你是如何传递价值你可以让我知道,它对我来说工作得很好 – newjenn

相关问题