2017-03-07 12 views
1

列「M」の数値が変更されたときに、列「D」から「L」をマージするように右のコードを作成しようとしています。列Mの変更が変更された場合のマージ範囲

私は以下のコードを持っていますが、列 "M"の値にかかわらず、すべての行が下から上へ行2にマージされます。

私は何が欠けていますか?

Sub Merge_Upon_Change() 
'Purpose: Merges cells between columns "D" and "L" when column "M" changes 

    Dim r As Long, i As Long 

    Application.DisplayAlerts = False 'Turn off windows warning popup 

    r = Cells(Rows.Count, "D").End(xlUp).row   ' find last cell in Column D 

     For i = r To 2 Step -1 
      If Cells(i, 13).Value <> Cells(i + 13, 13).Value Then 'upon change in column M = 13 
       Range("D" & i & ":L" & i).Merge      'then merge column "D" through "L" 

      End If 

     Next i 

    Application.DisplayAlerts = True ''Turn on Windows warning popup 

End Sub 
+0

おそらく 'Cells(i + 13、...'は 'Cells(i + 1、...'? –

+0

コードを 'If Cells(i、13).Value <>セル(i + 1,13).Valueに変更するトリックを行いました。ありがとう!!ちなみに – XLmatters

答えて

1

実はあなたは既にこの質問をしたが、私はこの質問についての将来の検索のためにこの回答を投稿しています未解決であることから、これをふりをします。私は13番目行があなたのから等しくないある+おそらくので

あなたはMとして、あなたの方程式を書き、私は <>M私は13を+、それは単に、すべての方程式を(見つけました私は行目)と、ここではループについてあなたのとして2行目の下からすべてをマージしている

まで
Sub Merge_Upon_Change() 
'Purpose: Merges cells between columns "D" and "L" when column "M" changes 

    Dim r As Long, i As Long 

    Application.DisplayAlerts = False 'Turn off windows warning popup 

    r = Cells(Rows.Count, "D").End(xlUp).row   ' find last cell in Column D 

     For i = r To 2 Step -1 
      If Cells(i, 13).Value <> Cells(i + 1, 13).Value Then 'upon change in column M = 13 
       Range("D" & i & ":L" & i).Merge      'then merge column "D" through "L" 

      End If 

     Next i 

    Application.DisplayAlerts = True ''Turn on Windows warning popup 

End Sub 
+0

@XLmattersところで、この例では、式**の問題内で**変数を正常に適用したことに気付きました。 – Mertinc

+0

フィードバックありがとう、Mertinc。私は3月7日に投稿されて以来、ちょっとした問題なしにこのサブ手順を毎週使い続けてきました。だから、それはかなりうまくテストされています。 – XLmatters

関連する問題