0

Attached you find a snippet of an Excel macro, in which the part in the for-loop always throws an error:

run-time error 1004 "application defined or object defined error"

I have checked online and it is advised to correct the object's qualification by adding a dot before the cells(xy) expression. However, if I change the code to Range(.Cells(xyz)), I am given another error:

invalid or unqualified reference

Any advice on how to fix this problem?

    Sub Run()

Dim n As Integer
n = Worksheets("Results").Cells(10, 8).Value
For i = 1 To n
    ActiveWorkbook.RefreshAll
    Worksheets("VBA").Range(Cells(1, i + 3), Cells(12, i + 3)).Value = Worksheets("Results").Range("F3:F14").Value
Next i

Worksheets("Results").Range("J3:L14").Value = Worksheets("VBA").Range("A1:C12").Value
Worksheets("VBA").Range(Cells(1, 4), Cells(12, i + 3)).Clear

End Sub
pnuts
  • 58,317
  • 11
  • 87
  • 139
seulberg1
  • 955
  • 1
  • 7
  • 19

1 Answers1

0

you need to fully qualify all cells and ranges.

When putting the "." before cells you need to be inside a with block as such:

With Worksheets("VBA")
    .Range(.Cells(1, i + 3), .Cells(12, i + 3)).Value = Worksheets("Results").Range("F3:F14").Value
End with

This basically is saying the everytime a function starts with "." it is the same as having Worksheets("VBA") in front: Worksheets("VBA").Cells(....). without all the typing.

Scott Craner
  • 148,073
  • 10
  • 49
  • 81