2 dimensional array

M

Mike

This is what i do for a 1 dimensional array but i wondering how
to do a 2 dimensional. Then have Range("M" & x) = the first dimension
and have Range("M" & x).offset(0,1) = the second dimension. Just starting to
understand arrays.

Dim arrEmployee() As Variant
Dim strEmployee As Variant
Dim rng As Range
Dim x As Long
Set rng = Range(Cells(2, 3), Cells(Rows.Count, 3).End(xlUp))
x = 0
ReDim arrEmployee(1 To rng.Rows.Count)
For Each cell In rng
x = x + 1
arrEmployee(x) = cell.Value
Next cell

x = 2
For Each strEmployee In arrEmployee
Range("M" & x) = strEmployee
x = x + 1
Next
 
D

Dave Peterson

You can actually pickup the value from a range and end up with a 2 dimensional
array.

This would create a 2 dimensional array (5 rows by 1 column)
dim myArr as variant
myArr = activesheet.range("a1:A5").value

This would create a 2 dimensional array (5 rows by 2 columns)
dim myArr as variant
myArr = activesheet.range("a1:b5").value

So maybe you could use something like:

dim myArr as variant
with activesheet
myArr = activesheet.range("C2:D" & .cells(.rows.count,"C").end(xlup).row).value
end with
 
J

Joel

Dim arrEmployee() As Variant
Dim strEmployee As Variant
Dim rng As Range
Dim x As Long
Set rng = Range(Cells(2, 3), Cells(Rows.Count, 3).End(xlUp))
x = 0
ReDim arrEmployee(1 To rng.Rows.Count,2)
For Each cell In rng
x = x + 1
arrEmployee(x,1) = cell.Value
arrEmployee(x,2) = cell.offset(0,1).Value
Next cell

x = 2
For EmployeeCount = 1 to Ubound(arrEmployee)
Range("M" & x) = arrEmployee(EmployeeCount,1)
Range("M" & x).offset(0,1) = arrEmployee(EmployeeCount,2)
x = x + 1
Next
 
M

Mike

Thanks Joel

Joel said:
Dim arrEmployee() As Variant
Dim strEmployee As Variant
Dim rng As Range
Dim x As Long
Set rng = Range(Cells(2, 3), Cells(Rows.Count, 3).End(xlUp))
x = 0
ReDim arrEmployee(1 To rng.Rows.Count,2)
For Each cell In rng
x = x + 1
arrEmployee(x,1) = cell.Value
arrEmployee(x,2) = cell.offset(0,1).Value
Next cell

x = 2
For EmployeeCount = 1 to Ubound(arrEmployee)
Range("M" & x) = arrEmployee(EmployeeCount,1)
Range("M" & x).offset(0,1) = arrEmployee(EmployeeCount,2)
x = x + 1
Next
 

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