André Silva
André Silva

Reputation: 7

Cannot set array in Cells()

I tried to create an array to put specific rows which I want a value, but when I try to execute it returns me error 1004

Option Explicit
Sub teste()

Dim i
Dim myarray
Dim ws As Worksheet
Dim aa

Set ws = ThisWorkbook.Sheets("Test")

myarray = Array(1, 2, 5, 7)


For i = LBound(myarray) To UBound(myarray)
    ws.Cells(i, 1).Value = 1
Next

End Sub

How can I resolve this?

Upvotes: 0

Views: 37

Answers (1)

Scott Craner
Scott Craner

Reputation: 152660

i in your case is a counter that goes from LBound(myarray) which is 0 and UBound(myarray) which is 3. i is not the value in the array. The value in the array is myarray(i)

So change the reference inside the loop:

Option Explicit
Sub teste()

Dim i
Dim myarray
Dim ws As Worksheet
Dim aa

Set ws = ThisWorkbook.Sheets("Test")

myarray = Array(1, 2, 5, 7)


For i = LBound(myarray)+1 To UBound(myarray)+1
    ws.Cells(myarray(i), 1).Value = 1
Next

End Sub

Upvotes: 2

Related Questions