2016-06-17 8 views
2

ブック内のすべてのワークシートの列B〜Fに数式を挿入するマクロを作成しようとしています。何らかの理由で、ブックのループが機能していないし、マクロが最初のブックで連続して実行されます。誰でもアイデアはありますか?マクロが期待通りのワークブックでループしていない

Option Explicit 
Sub Formuoli() 
Dim iLastRow As Integer 
Dim i As Integer 
Dim ws As Worksheet 
    iLastRow = Range("a10000").End(xlUp).Row 
For Each ws In ThisWorkbook.Worksheets 
    With ws 
     For i = 1 To iLastRow 
      Range("B" & i).Select 
      Selection.Formula = 'these are formulas 
      Range("C" & i).Select 
      Selection.Formula = 'these are formulas 
      Range("D" & i).Select 
      Selection.Formula = 'these are formulas 
      Range("E" & i).Select 
      Selection.Formula = 'these are formulas 
      Range("F" & i).Select 
      Selection.Formula = 'these are formulas 
     Next i 
    End With 
Next ws 
End Sub 
+4

たとえば、(i = 1〜iLastRowの後に) '.Range(" B "&i).Select'を指定する必要があります。 – Ralph

+0

また、私は[** .Select]の使用を避けるために、非常に**大いに**読んでおくことをお勧めします。 (http://stackoverflow.com/questions/10714251/how-to-avoid-using-select-in-excel-vba-macros)要するに、 '.Select'テキストを削除して' Selectionあなたが '.Range(" B "&i).Formula = ...'を実行できるように、 'Select 'がどこにあったかを指定してください。 – BruceWayne

答えて

4

あなたはRange関数の前にいくつかの点を逃した:

Option Explicit 

Sub Formuoli() 

Dim iLastRow As Integer 
Dim i As Integer 
Dim ws As Worksheet 

For Each ws In ThisWorkbook.Worksheets 
    With ws 
     iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row 
     For i = 1 To iLastRow 
      .Range("B" & i).Formula = "" 'these are formulas 
      .Range("C" & i).Formula = "" 'these are formulas 
      .Range("D" & i).Formula = "" 'these are formulas 
      .Range("E" & i).Formula = "" 'these are formulas 
      .Range("F" & i).Formula = "" 'these are formulas 
     Next i 
    End With 
Next ws 

End Sub 

変更:

  1. は、必要なドットを追加しました。
  2. iLastRowをループに移動して、各シートの最終行を決定します。
  3. Selectを削除して複数の行を結合しました。
+0

ありがとう! – Rob

関連する問題