ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Entering data into arrays (https://www.excelbanter.com/excel-programming/385259-entering-data-into-arrays.html)

Matt McQueen

Entering data into arrays
 
Folks,

I have a set of data like this:

1 Dave
3 Matt
2 Bill
4 Steve

I wanted to enter this data into an array, and assign it an address within
that array based on the number associated with each name. Hence 1 = Dave, 2 =
Bill etc. However the output seems to miss out the second data point. You end
up with:

1 Dave
2
3 Matt
4 Steve

Does data-entry into an array in VBA have to be sequential? The code I was
using was:

Sub test()
Dim testname(4)

For i = 1 To 4

testname(Cells(i, 1).Value) = Cells(i, 2).Value
Cells(i, 3).Value = testname(i)

Next i

End Sub

With the data in columns A and B in the spreadsheet. Appreciate it if
someone can spot the problem.

Cheers,

Matt


Jim Thomlinson

Entering data into arrays
 
Your array is populated just fine. Your issue is with when you try to write
the values. The second array item is not populated until your counter is at
3. Problem is you tried to write it at 2 before it was populated... try
this...

Sub test()
Dim testname(4) As String
Dim i As Integer

For i = 1 To 4
testname(Cells(i, 1).Value) = Cells(i, 2).Value
Next i

For i = 1 To 4
Cells(i, 3).Value = testname(i)
Next i
End Sub
--
HTH...

Jim Thomlinson


"Matt McQueen" wrote:

Folks,

I have a set of data like this:

1 Dave
3 Matt
2 Bill
4 Steve

I wanted to enter this data into an array, and assign it an address within
that array based on the number associated with each name. Hence 1 = Dave, 2 =
Bill etc. However the output seems to miss out the second data point. You end
up with:

1 Dave
2
3 Matt
4 Steve

Does data-entry into an array in VBA have to be sequential? The code I was
using was:

Sub test()
Dim testname(4)

For i = 1 To 4

testname(Cells(i, 1).Value) = Cells(i, 2).Value
Cells(i, 3).Value = testname(i)

Next i

End Sub

With the data in columns A and B in the spreadsheet. Appreciate it if
someone can spot the problem.

Cheers,

Matt



All times are GMT +1. The time now is 07:34 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com