2016-03-28 9 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"変数に格納されている式自体にアクセスすることです。次に、その数式のコンポーネントを連結で置き換える必要があります。第3に、 "&"シンボルを置き換えるべきか、実際には正当な文字列コンポーネントであるかを示すために、特定のフレーズ[Quote Start]と[Quote End]を追加して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 CONCATENATE( "A"、 ",,"、A、 ")の結果を返すA =" A ""& "A" A、A) –

+0

あなたはここで絶対に正しいです。これは、見積もりがこの中で説明されている場合に対処する必要があります。 – TsTeaTime

1

私は「CONCATENATE」式で、すべてのアンパサンドを置き換えるために、正規表現を使用します。A1 &「D:それはテキスト(例でアンパサンドを変換しないこと

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 

注意を& 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

関連する問題