Consulting

Results 1 to 3 of 3

Thread: HELP!!! Very Important For-Next Loop

  1. #1
    VBAX Newbie
    Joined
    Dec 2008
    Posts
    2
    Location

    HELP!!! Very Important For-Next Loop

    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

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Two things.

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

    [vba]

    FindStreetRow = Cells(intCount, 7)
    [/vba]

    or

    [vba]

    FindStreetRow = Cells(intCount, "G")
    [/vba]

    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.
    Last edited by Bob Phillips; 12-02-2008 at 01:56 AM.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Newbie
    Joined
    Dec 2008
    Posts
    2
    Location
    thanks a lot...i still have a few more questions but ill try to work them out on my own

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •