PC Review


Reply
Thread Tools Rate Thread

Assigning Variables To An array Throughout a loop

 
 
R Tanner
Guest
Posts: n/a
 
      17th Dec 2008
Hi,

I'm trying to assign a value to an array in the following procedure
and it is telling me 'Subscript out of range at the statement MyArray
(i) =whichchar. How can I do this differently to continue to assign
variables to this array throughout the loop?

Private Sub ParseData(MyWord As String)
Dim I As Integer, MyStep As Integer, LastLetter As String, NumChar As
Integer, WhichChar As String
Dim numOccurences As Integer, T As Integer
Dim MyArray() As String


MyStep = 1
I = 0
NumChar = 0

Do Until I = 1
LastLetter = Mid(MyWord, MyStep, 1)
MyStep = MyStep + 1
Select Case LastLetter
Case Is = ""
I = I + 1
End Select
DoEvents
Loop

NumChar = MyStep - 2

I = 1
T = 1

Do While I <= NumChar
WhichChar = Mid(MyWord, I, 1)
Do Until T = NumChar + 1
Select Case Mid(MyWord, T, 1)
Case Is = WhichChar
numoccurrences = numoccurrences + 1
T = T + 1
Case Else
T = T + 1
End Select

Select Case T
Case Is = NumChar + 1
MyArray(I) = WhichChar
Debug.Print "There are " & numoccurrences & " " &
WhichChar & "'s in this word"
End Select

Loop
T = 1
I = I + 1
numoccurrences = 0
Loop






End Sub
 
Reply With Quote
 
 
 
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Dec 2008
You declared the MyArray() array as a dynamic array (nothing between the
parentheses), but you didn't tell VB how many elements it will have. You do
that with a ReDim statement. Looking at your code, I *think* you will need
to add this line to your code...

ReDim MyArray(1 To Len(MyWord))

which will tell VB to reserve one element for each letter in the word being
passed into the subroutine. However, in looking at your code, you never use
the MyArray array, so I'm not sure why you created it or are attempting to
populate it. Also, you declared a variable with the name numOccurences, but
then went on to misspell it in the rest of your code.

Based on what the code you wrote does, you took a very complicated route to
achieve it. Here is a much shorter (and more efficient) subroutine which
does what your code currently does (note that I did not make use of the
MyArray array because you didn't)...

Sub ParseData(MyWord As String)
Dim X As Long
Dim WhichChar As String
Dim numOccurences As Long
For X = 1 To Len(MyWord)
WhichChar = Mid(MyWord, X, 1)
numOccurences = Len(MyWord) - Len(Replace(MyWord, WhichChar, ""))
Debug.Print "There are " & numOccurences & " " & _
WhichChar & "'s in this word"
Next
End Sub

I would like to make a suggestion to you that you become more familiar with
the various functions and statements that VB has to offer. Doing that will
allow you to write more focused code in the future.

--
Rick (MVP - Excel)


"R Tanner" <(E-Mail Removed)> wrote in message
news:98d28103-b1e8-472a-a260-(E-Mail Removed)...
> Hi,
>
> I'm trying to assign a value to an array in the following procedure
> and it is telling me 'Subscript out of range at the statement MyArray
> (i) =whichchar. How can I do this differently to continue to assign
> variables to this array throughout the loop?
>
> Private Sub ParseData(MyWord As String)
> Dim I As Integer, MyStep As Integer, LastLetter As String, NumChar As
> Integer, WhichChar As String
> Dim numOccurences As Integer, T As Integer
> Dim MyArray() As String
>
>
> MyStep = 1
> I = 0
> NumChar = 0
>
> Do Until I = 1
> LastLetter = Mid(MyWord, MyStep, 1)
> MyStep = MyStep + 1
> Select Case LastLetter
> Case Is = ""
> I = I + 1
> End Select
> DoEvents
> Loop
>
> NumChar = MyStep - 2
>
> I = 1
> T = 1
>
> Do While I <= NumChar
> WhichChar = Mid(MyWord, I, 1)
> Do Until T = NumChar + 1
> Select Case Mid(MyWord, T, 1)
> Case Is = WhichChar
> numoccurrences = numoccurrences + 1
> T = T + 1
> Case Else
> T = T + 1
> End Select
>
> Select Case T
> Case Is = NumChar + 1
> MyArray(I) = WhichChar
> Debug.Print "There are " & numoccurrences & " " &
> WhichChar & "'s in this word"
> End Select
>
> Loop
> T = 1
> I = I + 1
> numoccurrences = 0
> Loop
>
>
>
>
>
>
> End Sub


 
Reply With Quote
 
R Tanner
Guest
Posts: n/a
 
      17th Dec 2008
On Dec 16, 11:27*pm, "Rick Rothstein"
<rick.newsNO.S...@NO.SPAMverizon.net> wrote:
> You declared the MyArray() array as a dynamic array (nothing between the
> parentheses), but you didn't tell VB how many elements it will have. You do
> that with a ReDim statement. Looking at your code, I *think* you will need
> to add this line to your code...
>
> ReDim MyArray(1 To Len(MyWord))
>
> which will tell VB to reserve one element for each letter in the word being
> passed into the subroutine. However, in looking at your code, you never use
> the MyArray array, so I'm not sure why you created it or are attempting to
> populate it. Also, you declared a variable with the name numOccurences, but
> then went on to misspell it in the rest of your code.
>
> Based on what the code you wrote does, you took a very complicated route to
> achieve it. Here is a much shorter (and more efficient) subroutine which
> does what your code currently does (note that I did not make use of the
> MyArray array because you didn't)...
>
> Sub ParseData(MyWord As String)
> * Dim X As Long
> * Dim WhichChar As String
> * Dim numOccurences As Long
> * For X = 1 To Len(MyWord)
> * * WhichChar = Mid(MyWord, X, 1)
> * * numOccurences = Len(MyWord) - Len(Replace(MyWord, WhichChar, ""))
> * * Debug.Print "There are " & numOccurences & " " & _
> * * * * * * * * *WhichChar & "'s in this word"
> * Next
> End Sub
>
> I would like to make a suggestion to you that you become more familiar with
> the various functions and statements that VB has to offer. Doing that will
> allow you to write more focused code in the future.
>
> --
> Rick (MVP - Excel)
>
> "R Tanner" <tanner.ro...@gmail.com> wrote in message
>
> news:98d28103-b1e8-472a-a260-(E-Mail Removed)...
>
>
>
> > Hi,

>
> > I'm trying to assign a value to an array in the following procedure
> > and it is telling me 'Subscript out of range at the statement MyArray
> > (i) =whichchar. *How can I do this differently to continue to assign
> > variables to this array throughout the loop?

>
> > Private Sub ParseData(MyWord As String)
> > Dim I As Integer, MyStep As Integer, LastLetter As String, NumChar As
> > Integer, WhichChar As String
> > Dim numOccurences As Integer, T As Integer
> > Dim MyArray() As String

>
> > MyStep = 1
> > I = 0
> > NumChar = 0

>
> > Do Until I = 1
> > * *LastLetter = Mid(MyWord, MyStep, 1)
> > * *MyStep = MyStep + 1
> > * *Select Case LastLetter
> > * * * *Case Is = ""
> > * * * * * *I = I + 1
> > * *End Select
> > * *DoEvents
> > Loop

>
> > NumChar = MyStep - 2

>
> > I = 1
> > T = 1

>
> > Do While I <= NumChar
> > * *WhichChar = Mid(MyWord, I, 1)
> > * * * *Do Until T = NumChar + 1
> > * * * * * *Select Case Mid(MyWord, T, 1)
> > * * * * * * * *Case Is = WhichChar
> > * * * * * * * * * *numoccurrences = numoccurrences + 1
> > * * * * * * * * * *T = T + 1
> > * * * * * * * *Case Else
> > * * * * * * * * * *T = T + 1
> > * * * * * *End Select

>
> > * * * * * *Select Case T
> > * * * * * * * *Case Is = NumChar + 1
> > * * * * * * * * * *MyArray(I) = WhichChar
> > * * * * * * * * * *Debug.Print "There are " & numoccurrences & " " &
> > WhichChar & "'s in this word"
> > * * * * * *End Select

>
> > * * * *Loop
> > * * * *T = 1
> > * * * *I = I + 1
> > * * * *numoccurrences = 0
> > Loop

>
> > End Sub- Hide quoted text -

>
> - Show quoted text -


the len() function is what I was looking for...How silly of me...by
the way, I did use the myarray in the following section of the above -
posted code...


Select Case T
Case Is = NumChar + 1
MyArray(I) = WhichChar
Debug.Print "There are " & numoccurrences & " " &
WhichChar & "'s in this word"
End Select



 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      17th Dec 2008

> the len() function is what I was looking for...How silly of me...
> by the way, I did use the myarray in the following section of
> the above - posted code...
>
>
> Select Case T
> Case Is = NumChar + 1
> MyArray(I) = WhichChar
> Debug.Print "There are " & numoccurrences & " " &
> WhichChar & "'s in this word"
> End Select


Yes, but other than assigning values to it, you never made use the array
again.

--
Rick (MVP - Excel)



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop through Non-Array Variables Steve Microsoft VB .NET 5 13th Apr 2008 11:59 AM
Assigning Array variables to a list box matthew.macdonald-wallace@fujifilmsericol.com Microsoft Dot NET 1 3rd Nov 2006 04:41 PM
select variables ranges, copy to array, paste the array in new workbook Mathew Microsoft Excel Worksheet Functions 1 1st Apr 2005 09:40 AM
Re: Assigning 10x1 array to 2nd collumn of 10x3 array Alan Beban Microsoft Excel Programming 0 30th Jul 2004 01:38 AM
Re: Assigning 10x1 array to 2nd collumn of 10x3 array Myrna Larson Microsoft Excel Programming 0 29th Jul 2004 11:57 PM


Features
 

Advertising
 

Newsgroups
 


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