create array

A

Al

There must be a better way of doing this....

Dim vv(10) As Integer
vv(1) = 1
vv(1) = 3
vv(1) = 4
vv(1) = 5
vv(1) = 7
vv(1) = 17
vv(1) = 18
vv(1) = 19
vv(1) = 20
vv(1) = 21
vv(1) = 22

Can't I do something like:
Dim vv(10)
vv() = (1,3,4,5,7,.....)

Al
 
G

Gary Keramidas

maybe something like this?

Option Base 1
Sub test()
Dim vv As Variant
vv = Array(1, 3, 4, 5, 7, 17, 18, 19, 20, 21, 22)
MsgBox vv(1)
End Sub
 
C

Chip Pearson

Al,

You could do something like

Dim N As Long
For N = LBound(vv) To UBound(vv)
vv(N) = N
Next N

--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
A

Al

I'll try both, thanks.

Chip, how do I specify that N is to be the values I mentioned above,
considering they're not in sequence?
 
C

Chip Pearson

I didn't notice that they were not sequential. Ignore my post and go with
Gary's.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting
www.cpearson.com
(email on the web site)
 
G

Guest

I suspect that since your example assigned all the values to the same
element of the array, overwriting the previous assignment, Chip gave you a
generic approach.

Dim v as Variant
vv = Evaluate("{1,3,4,5,7,17,18,19,20,21,22}")


or building on Chips Example

Sub A()
Dim vv(0 To 10)
j = 0
For i = 1 To 7
If i <> 2 And i <> 6 Then
vv(j) = i
j = j + 1
End If
Next
For i = 17 To 22
vv(j) = i
j = j + 1
Next

For i = LBound(vv) To UBound(vv)
Debug.Print i, vv(i)
Next

End Sub
 
D

Dana DeLouis

Sub A()
For i = 1 To 7
For i = 17 To 22

Given an Excel table, one of the programs I have gave the following 1-liner
as one of its suggestions:

Sub Demo()
Dim v
v = [Transpose(If(Row(1:11)<=5,Mod(1030, 6*Row(1:11)+1),11+Row(1:11)))]
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top