PDA

View Full Version : HELP!!! Very Important For-Next Loop



Glass
12-02-2008, 12:14 AM
I have been working on VBA for my Computer Science class and I cannot get this to work. This is what I need to do:

The FindStreetRow function has a street name(a string) as its parameter and returns the row (an integer) correspoding to that street in cells G10:G24 in the Game worksheet.
If it can't find the street name in cells G10:G24, it will return 0. To write the code for this fuction, you first need to write a proper function declaration for FindStreetRow. (Hint:
You have seen a function declaration in Part I.) Then, write the following code inside your function declaration:
1. Declare a local integer variable that will be used as the counter variable of a For Next loop.
Use a For Next loop to iterate though each row in G10:G24.
Set the start and end values of the counter variable properly to iterate each row in G10:G24. Thus, the counter variable will refer to the rows of all the street one by
one.
1.
Use an If statement to check if the parameter of the function matches the street name in the cell with column index 7 (column G) and row index stored in the
counter variable.
If there is a match, return the value of the counter variable and exit.
(Hint: the code for exiting is Exit Function, and comparison in VBA is case sensitive.)
2.
2.
3. Return a value 0 in FindStreetRow.

This is what I have:
Function FindStreetRow(strStreetName As String) As Integer
Dim intCount As Integer
For intCount = 10 To 24
FindStreetRow = Cells(intCount, "7")
If FindStreetRow = Cells("intCount, 7").Value Then
FindStreetRow = intCount
Else
FindStreetRow = 0
End If
Next intCount
End Function

Any help quickly would be GREATLY appreciated

Bob Phillips
12-02-2008, 01:28 AM
Two things.

When using the Cells property, you either use the column number or the column letter, that is



FindStreetRow = Cells(intCount, 7)


or



FindStreetRow = Cells(intCount, "G")


Secondly, you should set the default return value to 0 , before the loop starts, and if and when you meet a match, exit the loop, otherwise you just return the result of checking the last row.

Glass
12-02-2008, 01:50 AM
thanks a lot...i still have a few more questions but ill try to work them out on my own