2016-06-27 5 views
0

次のステップを適用する必要がある大きな範囲のデータ(320行と3列)をコピーするマクロがあります。行2を切り取り、行9を削除し、行4を削除します。このデータ範囲には別のパターンがあります。Excel専用のカット/ペースト、自動化が必要なパターンを削除する

this pattern

このパターンは、さまざまな情報が含まれている40個のファイルからコピーされますが、同じように構成されています。 40個のファイルのそれぞれには8つの行がコピーされています(写真の異なる色で色付けされています)。私はカット/ペーストし、各グループの特定の行を削除して40行にする必要があります。ここで私はそれを現在行う方法です。

Rows("2:2").cut 
    Rows("9:9").Insert Shift:=xlDown 
    Rows("4:4").Delete Shift:=xlUp 
    Rows("9:9").cut 
    Rows("16:16").Insert Shift:=xlDown 
    Rows("11:11").Delete Shift:=xlUp 
    Rows("16:16").cut 
    Rows("23:23").Insert Shift:=xlDown 
    Rows("18:18").Delete Shift:=xlUp 
    Rows("23:23").cut 
    Rows("30:30").Insert Shift:=xlDown 
    Rows("25:25").Delete Shift:=xlUp 
    Rows("30:30").cut 
    Rows("37:37").Insert Shift:=xlDown 
    Rows("32:32").Delete Shift:=xlUp 
    Rows("37:37").cut 
    Rows("44:44").Insert Shift:=xlDown 
    Rows("39:39").Delete Shift:=xlUp 
    Rows("44:44").cut 
    Rows("51:51").Insert Shift:=xlDown 
    Rows("46:46").Delete Shift:=xlUp 
    Rows("51:51").cut 
    Rows("58:58").Insert Shift:=xlDown 
    Rows("53:53").Delete Shift:=xlUp 
    Rows("58:58").cut 
    Rows("65:65").Insert Shift:=xlDown 
    Rows("60:60").Delete Shift:=xlUp 
    Rows("65:65").cut 
    Rows("72:72").Insert Shift:=xlDown 
    Rows("67:67").Delete Shift:=xlUp 
    Rows("72:72").cut 
    Rows("79:79").Insert Shift:=xlDown 
    Rows("74:74").Delete Shift:=xlUp 
    Rows("79:79").cut 
    Rows("86:86").Insert Shift:=xlDown 
    Rows("81:81").Delete Shift:=xlUp 
    Rows("86:86").cut 
    Rows("93:93").Insert Shift:=xlDown 
    Rows("88:88").Delete Shift:=xlUp 
    Rows("93:93").cut 
    Rows("100:100").Insert Shift:=xlDown 
    Rows("95:95").Delete Shift:=xlUp 
    Rows("100:100").cut 
    Rows("107:107").Insert Shift:=xlDown 
    Rows("102:102").Delete Shift:=xlUp 
    Rows("107:107").cut 
    Rows("114:114").Insert Shift:=xlDown 
    Rows("109:109").Delete Shift:=xlUp 
    Rows("114:114").cut 
    Rows("121:121").Insert Shift:=xlDown 
    Rows("116:116").Delete Shift:=xlUp 
    Rows("121:121").cut 
    Rows("128:128").Insert Shift:=xlDown 
    Rows("123:123").Delete Shift:=xlUp 
    Rows("128:128").cut 
    Rows("135:135").Insert Shift:=xlDown 
    Rows("130:130").Delete Shift:=xlUp 
    Rows("135:135").cut 
    Rows("142:142").Insert Shift:=xlDown 
    Rows("137:137").Delete Shift:=xlUp 
    Rows("142:142").cut 
    Rows("149:149").Insert Shift:=xlDown 
    Rows("144:144").Delete Shift:=xlUp 
    Rows("149:149").cut 
    Rows("156:156").Insert Shift:=xlDown 
    Rows("151:151").Delete Shift:=xlUp 
    Rows("156:156").cut 
    Rows("163:163").Insert Shift:=xlDown 
    Rows("158:158").Delete Shift:=xlUp 
    Rows("163:163").cut 
    Rows("170:170").Insert Shift:=xlDown 
    Rows("165:165").Delete Shift:=xlUp 
    Rows("170:170").cut 
    Rows("177:177").Insert Shift:=xlDown 
    Rows("172:172").Delete Shift:=xlUp 
    Rows("177:177").cut 
    Rows("184:184").Insert Shift:=xlDown 
    Rows("179:179").Delete Shift:=xlUp 
    Rows("184:184").cut 
    Rows("191:191").Insert Shift:=xlDown 
    Rows("186:186").Delete Shift:=xlUp 
    Rows("191:191").cut 
    Rows("198:198").Insert Shift:=xlDown 
    Rows("193:193").Delete Shift:=xlUp 
    Rows("198:198").cut 
    Rows("205:205").Insert Shift:=xlDown 
    Rows("200:200").Delete Shift:=xlUp 
    Rows("205:205").cut 
    Rows("212:212").Insert Shift:=xlDown 
    Rows("207:207").Delete Shift:=xlUp 
    Rows("212:212").cut 
    Rows("219:219").Insert Shift:=xlDown 
    Rows("214:214").Delete Shift:=xlUp 
    Rows("219:219").cut 
    Rows("226:226").Insert Shift:=xlDown 
    Rows("221:221").Delete Shift:=xlUp 
    Rows("226:226").cut 
    Rows("233:233").Insert Shift:=xlDown 
    Rows("228:228").Delete Shift:=xlUp 
    Rows("233:233").cut 
    Rows("240:240").Insert Shift:=xlDown 
    Rows("235:235").Delete Shift:=xlUp 
    Rows("240:240").cut 
    Rows("247:247").Insert Shift:=xlDown 
    Rows("242:242").Delete Shift:=xlUp 
    Rows("247:247").cut 
    Rows("254:254").Insert Shift:=xlDown 
    Rows("249:249").Delete Shift:=xlUp 
    Rows("254:254").cut 
    Rows("261:261").Insert Shift:=xlDown 
    Rows("256:256").Delete Shift:=xlUp 
    Rows("261:261").cut 
    Rows("268:268").Insert Shift:=xlDown 
    Rows("263:263").Delete Shift:=xlUp 
    Rows("268:268").cut 
    Rows("275:275").Insert Shift:=xlDown 
    Rows("270:270").Delete Shift:=xlUp 
    Rows("275:275").cut 
    Rows("282:282").Insert Shift:=xlDown 
    Rows("277:277").Delete Shift:=xlUp 

LASTROWを見つけ、それが今であるとしてハード、これらの各機能を持っていないことにより、自動的にこれを行う方法はありますか?どんな助けでも大歓迎です。行を削除する場合

答えて

0

は7

Sub moveRows() 
    Dim i As Long 
    Dim LastRow As Long 
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row 
    For i = 2 To LastRow Step 7 
     'Do your stuff with rows i, i+7 and i+2 here 
     LastRow = LastRow - 1 'taken from Nan Avan Illai. The result won't differ but it's "cleaner" that way 
    Next i 
End Sub 

のステップでインクリメントループを使用し、また時々に後方に行くことが役に立つことができます(Step -8を使用)ので、あなたはシフト行番号を混在させないでください。また、データのインポートを自動化し、インポート中にデータを編集することも検討する価値があります。

+0

下回ってみてください!迅速な対応をありがとう。 私はすでにインポートと編集を自動化しています。私はちょうどこの部分に問題がありました。マクロ全体を見ることができますhttp://codereview.stackexchange.com/questions/132825/consolidation-macro-for-excel – Shakdun

0

が魅力のように働いたコード

Sub test() 
    Dim last, i As Long 
    last = Range("A" & Rows.Count).End(xlUp).Row 
    For i = 2 To last Step 7 
     Range(Cells(i, 1), Cells(i, Columns.Count)).Cut 
     Range("A" & (i + 7)).Insert Shift:=xlDown 
     Range(Cells(i + 7, 1), Cells(i + 7, Columns.Count)).Delete Shift:=xlUp 
     last = last - 1 
    Next i 
End Sub 
+0

ありがとうございました! – Shakdun

+0

Ah最後の行の変数を減らすのを忘れました。違いはありませんが(おそらく)、はるかに徹底しています。 – arcadeprecinct

+0

@Shakdunは=私はそれが私のためにこの 'LASTROW =細胞(Rows.Count、1).END(xlUp).Row のように見える必要な行を削除/ /ペーストをカットするための値を少し微調整した後 –

関連する問題