2017-12-08 2 views
0

私は行2のすべての関連する列を満たしている数式の配列を持っています。その後、ダイナミックな参照で範囲を埋めるコードがあります。この特定のデータセットは74行のデータを有するが、何らかの理由で毎回65行目に一見、任意の時間にVBAが異常に出る?

438 Error: 'Object doesn't support this property or method'

を受信する。

 'Find last row/col variables 
     Dim lastRow As Long 
     Dim lastCol As Long 
     'Array variable 
     Dim TestFormulas() As Variant 
     'WS variable 
     Dim WS as Worksheet 

    lastRow = Cells.Find(What:="*", _ 
        After:=Range("A1"), _ 
        LookAt:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row 

lastCol = Cells.Find(What:="*", _ 
        After:=Range("A1"), LookAt:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByColumns, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row 

'Create Array [this is cut down significantly. There are roughly 50 formulas here. Code runs quickly, if it matters.] 
TestFormulas() = Array(_ 
        "=Sheet1!H2", "=Sheet1!B2", "=IF(Sheet1!P2=""Resiliency""") 

'Fill Row 2 with formulas 
With WS 
    For i = LBound(TestFormulas()) To UBound(TestFormulas()) 
     .Cells(2, 1 + i).Formula = TestFormulas(i) 
    Next i 
End With 

'Copy formulas and fill down the entire range 
Range("A2:" & lastCol & ":" & "2").Formula = TestFormulas 
Range("A2:" & WS(1).lastCol & ":" & WS(1).lastRow).FillDown 

私は、ダイナミック式で埋めるために範囲を期待しています。 `lastcol数であるかの唯一までを含む行65

438 Error: 'Object doesn't support this property or method'

+1

'範囲(:&lastCol& "" & "2" "A2")、それは、dynmicallyことで、充填します有効な範囲アドレスではない 'A2:52:2'と連結します。 Rangeオブジェクトの中で数値列を受け入れる 'Cells()'を使う必要があります。 –

+0

また、 '... Formula = [array of formulas]'を実行できることは確かですか?ループする必要があるかもしれません。 – BruceWayne

答えて

1
Dim lastRow As Long 

'Array variable 
Dim TestFormulas() As Variant 
'WS variable 
Dim WS As Worksheet 

Set WS = Worksheets("Sheet4") ' change to your sheet name 

With WS 
    If Application.WorksheetFunction.CountA(Worksheets("Sheet1").Cells) > 0 Then 
     lastRow = Worksheets("Sheet1").Cells.Find(What:="*", _ 
      After:=Range("A1"), _ 
      LookAt:=xlPart, _ 
      LookIn:=xlFormulas, _ 
      SearchOrder:=xlByRows, _ 
      SearchDirection:=xlPrevious, _ 
      MatchCase:=False).Row 


     'Create Array [this is cut down significantly. There are roughly 50 formulas here. Code runs quickly, if it matters.] 
     TestFormulas() = Array(_ 
     "=Sheet1!H2", "=Sheet1!B2", "=IF(Sheet1!P2=""Resiliency"",TRUE,FALSE)") 

     'Fill Row 2 with formulas 

     For i = LBound(TestFormulas()) To UBound(TestFormulas()) 
      .Range(.Cells(2, 1 + i), .Cells(lastRow, 1 + i)).Formula = TestFormulas(i) 
     Next i 
    End If 
End With 
+0

もう一度、スコット!私は行番号1全体にこのコードを入力することしかできません。違いがある場合は、すでに新しいシートを作成してアクティブ化するように設定しているので、Set WS = Worksheets( "Sheet4")行は使用しませんでした。 – Gurrito

+0

別のシートの最後の行を探していますか?編集を参照してください。 –

+0

ああ、大丈夫です。私は私の誤りを見る。それは、前のシートのデータを考慮するのではなく、その1行のデータを計算し、それに基づいて充填することだけでした。ありがとうございます! – Gurrito

関連する問題