ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   What is wrong with my array? (https://www.excelbanter.com/excel-programming/300984-what-wrong-my-array.html)

BLB

What is wrong with my array?
 
I have written the following code that gives a compiling erro
"subscript out of range". Where am I going wrong?

It is happening in the FOR loop of the "GetName" procedure. What I a
trying to do is get input from the user on how many people will b
added to the spreadsheet. If for example, they enter 2, than an inpu
box will pop up 2 times asking for the names of those people and wil
store those 2 names in the Name() array. I have tried 2 differen
loops, actually, and both are included in the code below. Neither on
works.

Private Sub AddStaff_Click()

Dim qty As Integer
Dim Name() As String

Call GetQty(qty)
Call GetName(Name, qty)

End Sub
__________________________

Private Sub GetQty(q)
q = InputBox("How many people are you adding? ", "Staff Quantity")
End Sub
__________________________
Private Sub GetName(n, ByVal q)

Dim i as Variant
Dim temp As String
Dim ctr As Integer
ctr = 0

///////I HAVE TRIED THIS LOOP////////////
Do Until ctr = q
temp = InputBox("Please enter the name of the person you ar
adding.", "Staff Name")
n(ctr) = temp
ctr = ctr + 1
Loop
//////////////////////////////////////////////////

//////////AND I HAVE TRIED THIS LOOP/////////
For i = ctr To q
temp = InputBox("Please enter the name of the person you ar
adding.", "Staff Name")
n(i) = temp
ctr = ctr + 1
Next i
////////////////////////////////////////////////////////////////
End Sub

Please Help.

Thank

--
Message posted from http://www.ExcelForum.com


RWN

What is wrong with my array?
 
You have declared your array as dynamic and have to "Redim" it in your
routine before you add an element.

--
Regards;
Rob
------------------------------------------------------------------------
"BLB " wrote in message
...
I have written the following code that gives a compiling error
"subscript out of range". Where am I going wrong?

It is happening in the FOR loop of the "GetName" procedure. What I am
trying to do is get input from the user on how many people will be
added to the spreadsheet. If for example, they enter 2, than an input
box will pop up 2 times asking for the names of those people and will
store those 2 names in the Name() array. I have tried 2 different
loops, actually, and both are included in the code below. Neither one
works.

Private Sub AddStaff_Click()

Dim qty As Integer
Dim Name() As String

Call GetQty(qty)
Call GetName(Name, qty)

End Sub
__________________________

Private Sub GetQty(q)
q = InputBox("How many people are you adding? ", "Staff Quantity")
End Sub
__________________________
Private Sub GetName(n, ByVal q)

Dim i as Variant
Dim temp As String
Dim ctr As Integer
ctr = 0

///////I HAVE TRIED THIS LOOP////////////
Do Until ctr = q
temp = InputBox("Please enter the name of the person you are
adding.", "Staff Name")
n(ctr) = temp
ctr = ctr + 1
Loop
//////////////////////////////////////////////////

//////////AND I HAVE TRIED THIS LOOP/////////
For i = ctr To q
temp = InputBox("Please enter the name of the person you are
adding.", "Staff Name")
n(i) = temp
ctr = ctr + 1
Next i
////////////////////////////////////////////////////////////////
End Sub

Please Help.

Thanks


---
Message posted from http://www.ExcelForum.com/




BLB

What is wrong with my array?
 
Just what I needed! Thanks Rob

--
Message posted from http://www.ExcelForum.com



All times are GMT +1. The time now is 12:22 PM.

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