2016-10-13 2 views
1

私はこれについて以前の投稿を見て、それを適用しようとしましたが、私はうまくいっていません。動的行をループするための最も速い方法

Sub test() 

Dim i As Long 
Dim varray As Variant 

Sheets("Original").Select 
varray = Sheets("Original").Range("A10:A" & Cells(Rows.Count, "A").End(xlUp).Row).Value 

For i = 10 To UBound(varray, 1) 
    If Cells(i, 16).Value <> "" Then 
     Cells(i + 1, 16).EntireRow.Insert 
     Cells(i + 1, 1).EntireRow.Value = Cells(i, 1).EntireRow.Value 
     Cells(i + 1, 6).Value = Cells(i, 16).Value 
     Cells(i + 1, 1).Value = 20305 
     Cells(i + 1, 11).Value = "" 
     Cells(i + 1, 12).Value = "" 
     Cells(i + 1, 15).Value = "" 
     Cells(i + 1, 16).Value = "" 
    End If 
Next 

End Sub 

これは、ループのために全体をスキップしてサブを終了するために行きます。どんな援助?

ありがとうございました

+0

データの開始行は何ですか? 10? – User632716

+0

コードをステップ実行すると、 'varray'の値は何ですか? –

+0

@tomprestonはい行10で始まる – user1

答えて

0

コードにはいくつか問題があります。私は下のスクリプトでそれらに対処しようとしました。残念ながら、あなたが作業しているデータの例はありません。見て、何かが動作していない場合は私に知らせる。

Option Explicit 

Sub test() 

    'Get used to declaring your worksheet 
    'and then reference that worksheet when wanting to access data form it. 
    Dim OrigSht As Worksheet 
    Set OrigSht = ThisWorkbook.Sheets("Original") 

    Dim LastRowColA As Long 
    LastRowColA = OrigSht.Cells(Rows.Count, "A").End(xlUp).Row 

    'Not sure why you wanted to use an Array, seeing as you dont use it in the loop. 
    'Unless you use it in some other code and this is a extract from other code. 
    Dim varray As Variant 
    varray = OrigSht.Range("A10:A" & LastRowColA).Value 

    'Using UBound could present errors if there was less than 10 lines of data _ 
    it would then step the loop because the to value is less than the start 
    'Rather use the last row of column A as a end of the For loop 

    'The problem with editing a list of data form the begining of a list _ 
    is that the list becomes longer as you add information, so when adding _ 
    or deleting lines you always want to start at the end og the list 
    Dim i As Long 
    For i = LastRowColA To 10 Step -1 

     With OrigSht 
      If .Cells(i, 16).Value <> "" Then 

       .Cells(i + 1, 16).EntireRow.Insert 
       .Cells(i + 1, 1).EntireRow.Value = .Cells(i, 1).EntireRow.Value 
       .Cells(i + 1, 6).Value = .Cells(i, 16).Value 
       .Cells(i + 1, 1).Value = 20305 
       .Cells(i + 1, 11).Value = "" 
       .Cells(i + 1, 12).Value = "" 
       .Cells(i + 1, 15).Value = "" 
       .Cells(i + 1, 16).Value = "" 
      End If 

     End With 
    Next 

End Sub 
関連する問題