2016-07-22 4 views
0

私はビジュアルベーシックを使用する初心者ですが、少し問題があります。私がしたいのは、Excelを使ってスプレッドシートを検索し、特定の列を検索して名前を見つけ、その行のすべてを取得して別のシートに転送することです。私は、私が理解していない理由のために無限ループに遭遇する以外はすべて動いている。ビジュアルベーシックで充満した細胞を検索する

'set up a for loop that increments through all sheets in the workbook 
For i = 1 To ThisWorkbook.Sheets.Count 
'set up a temp page to work with the current page 
    Set tem = ThisWorkbook.Sheets(i) 
    'increment through all the rows that have data in them 
     For Each rng In tem.Rows 
     'if the data matches what was searched for, copy it into another worksheet 
      If tem.Cells(ct, 4) = SForm.Text Then 
       sr.Cells(spot, 1) = tem.Cells(ct, 1) 
       sr.Cells(spot, 2) = tem.Cells(ct, 2) 
       sr.Cells(spot, 3) = tem.Cells(ct, 3) 
       sr.Cells(spot, 4) = tem.Cells(ct, 4) 
       sr.Cells(spot, 5) = tem.Cells(ct, 5) 
       sr.Cells(spot, 6) = tem.Cells(ct, 6) 
       sr.Cells(spot, 7) = tem.Cells(ct, 7) 
       sr.Cells(spot, 8) = tem.Cells(ct, 8) 
       sr.Cells(spot, 9) = tem.Cells(ct, 9) 
       sr.Cells(spot, 10) = tem.Cells(ct, 10) 
       sr.Cells(spot, 11) = tem.Cells(ct, 11) 
       sr.Cells(spot, 12) = tem.Cells(ct, 12) 

       'increment the placeholder for the new sheet 
       spot = spot + 1 
      End If 
      'increase ct to keep track of where in the worksheet it is 
      ct = ct + 1 
     Next rng 
     'reset ct for the next worksheet 
     ct = 1 
Next i 

私が実行する特定の問題は、ctがintであるためオーバーフローです。私は私の手の上に無限のループを持っていると私に伝えます。

ご協力ありがとうございます。

+0

1シートあたり100万回以上繰り返していますが、これには時間がかかります。さらに、 'ct'は必要ありません。その代わりに 'rng.row'を使用してください。 –

+0

[このリンク](http://www.globaliconnect.com/excel/index.php?option=com_content&view=article&id=112:find-method-in-excel-vba-find-multiple-occurrences-find-method -to-vlookup-find-date&catid = 79&Itemid = 475)**例 - 範囲内の値の複数の出現の検索**。ここでは大いに役立つでしょう。また、無限ループはありません。コードは、そこにデータがあるかどうかにかかわらず、**ワークシートのすべての行**をチェックするために書き込まれます。したがって、行が〜38,562(正確な手数はない)より大きくなると、 'Integer'変数はもはやそれを保持することができなくなります。 –

答えて

0

1シートあたり100万回以上繰り返していますが、これには時間がかかります。列Dの最後のセルを見つけ、それらの行だけを反復する

さらに、ctは必要ありません。その代わりにrng.rowを使用してください。

次に、値の割り当て全体を1行にまとめます。

l
For i = 1 To ThisWorkbook.Sheets.Count 
'set up a temp page to work with the current page 
Set tem = ThisWorkbook.Sheets(i) 
'increment through all the rows that have data in them 

    For Each rng In tem.Range("D1", tem.Cells(tem.Rows.Count, 4).End(xlUp)) 
    'if the data matches what was searched for, copy it into another worksheet 
     If rng.Value = SForm.Text Then 
      sr.Range(sr.Cells(spot, 1), sr.Cells(spot, 12)).Value = tem.Range(tem.Cells(rng.Row, 1), tem.Cells(rng.Row, 12)).Value 
      'increment the placeholder for the new sheet 
      spot = spot + 1 
     End If 
    Next rng 
Next i 

シートとvbaの間のやりとりを最小限に抑えるために、範囲全体を配列にドロップして別の配列に出力することがより迅速になります。

+0

あなたの提案を使用してプログラムを再実行しました。データをコピーする際に問題があるようです。どちらの範囲のセルの1つが空白だった場合、使用するメソッドに問題はありませんか?もしそうなら、これを回避する方法がありますか?私はオブジェクトの "メソッドの範囲" _ワークシート "失敗"エラーを取得しています。私は、それがコピーできないもの、または特定の場所にコピーできないものを見つけることを意味していると仮定します。 – Rejera

+0

@Rejera nope申し訳ありません、私のエラー。私は適切な親にCells()を修飾することを忘れました。編集を参照してください。画面を更新する必要があります。 –

+0

右、右。私はそれを見たはずです。今の魅力のように動作します。ありがとうございました。 – Rejera