0

I'm a bit new to VBA and don't understand how do I assign specific values into array, not the range. I'm trying to do something like this:

Dim toDel(8) As Integer
Set toDel() = Array(1, 3, 5, 6, 7, 8, 10, 12)

But it returns me error. So can I somehow insert a hole array into array without looping through each element? Like we do it in python just array = array

kstamax
  • 69
  • 9
  • 3
    `Dim toDel As variant` – Scott Craner Jul 20 '21 at 19:44
  • 3
    and `toDel = Array(1, 3, 5, 6, 7, 8, 10, 12)` – Scott Craner Jul 20 '21 at 19:44
  • 3
    `Array()` is a function that returns a Variant array, so you can only assign its result to a Variant. If you had a function that returns a strongly typed integer array, you could assign that to an integer array variable, provided it was declared as a [dynamic array](https://stackoverflow.com/a/8844294/11683). You should not use `Set`. – GSerg Jul 20 '21 at 19:58

1 Answers1

3

Consider:

Option Explicit
Sub whatever()
    Dim toDel As Variant
    toDel = Array(1, 3, 5, 6, 7, 8, 10, 12)
End Sub
Gary's Student
  • 95,722
  • 10
  • 59
  • 99