+ Reply to Thread
Results 1 to 8 of 8

lines in loop don't execute when program is run

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2012
    Location
    Chicago IL
    MS-Off Ver
    Excel 2010
    Posts
    15

    lines in loop don't execute when program is run

    Hello,

    I am working a program that is supposed to take a value the user inputs, search a database for it, and within all the entries that have that value, find another user input quality. I'm having trouble with my second loop. The code is below:
    Sub FindRowToInsert()
        Dim EngyrEnter As String, DbEngyr As String
        Dim Location As Integer, i As Integer, Row As Integer
        Dim Yes As Boolean
        Yes = False
    
        
    
        'loop goes through all occupied rows
        For i = 1 To [A65536].End(xlUp).Row
            
            'If the cell value is the specified power family,
            If Trim(Cells(i, 1).Value) = InputForm.cboPwrFam.Value Then
                Dim StartIndex As Range
                Set StartIndex = Cells(i, 1)
                
    
                Dim EngSizeEnter As String
                EngSizeEnter = Mid(InputForm.txtEngFam.Value, 6, 4)
                'loop looks through all rows within specified power family
                For Row = i To (i + (NumInFam - 1))
                    
                    Dim DbEngSize As String
                    DbEngSize = Mid(Cells(Row, 2).Value, 6, 4)
                   
                    'If the new engine size is smaller than the cells engine size, inserts engine
                    If EngSizeEnter < DbEngSize And Mid(Cells(i + Row, 2).Text, 10, 3) = Mid(InputForm.txtEngFam.Text, 6, 4) Then
                            Cells(1, i + Row).Select
                            PutInEngine
                            Yes = True
                    End If
                    'If new engine size is equal to cells engine size and type is the same,
                    If EngSizeEnter = DbEngSize And Mid(Cells(i + Row, 2).Text, 10, 3) = Mid(InputForm.txtEngFam.Text, 6, 4) Then
                        EngyrEnter = Mid(InputForm.txtEngFam.Text, 1, 1)
                        DbEngyr = Mid(Cells(i + Row, 2).Value, 1, 1)
                        'and the year is equal
                        If EngyrEnter = DbEngyr Then
                            'Inserts engine
                            Cells(i + Row, 2).Select
                            PutInEngine
                            Yes = True
                        Else
                            Yes = False
                        End If
                    End If
                    'If new engine size is greater than cells engine then goes to next row
                    If EngSizeEnter > DbEngSize Then
                        Row = Row + 1
                    End If
                Next Row
                'If loop has completed and engine has not been inserted, inserts at last row of power family
                If Yes = False Then
                    Cells(i + NumInFam, 1).Select
                    PutInEngine
                End If
            End If
        Next i
    End Sub
    I'm not getting any compile or runtime errors, but while stepping through the program, I found that the program will correctly execute this line:
                EngSizeEnter = Mid(InputForm.txtEngFam.Value, 6, 4)
    which is inside the first loop and outside of the second loop, but not this line of code:
    DbEngSize = Mid(Cells(Row, 2).Value, 6, 4)
    and will therefore not perform any of the if statements after this line. After it executes the "For Row =" line, it goes directly to "if yes = false"

    Any ideas why this could be happening?

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: lines in loop don't execute when program is run

    Row is a Excel reserve keyword. Try changing all references to iRow.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    08-08-2012
    Location
    Chicago IL
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: lines in loop don't execute when program is run

    Thanks David, I made that change, but it is still skipping from the start of the loop to the if statement after it. Anything else I may not have thought of?

  4. #4
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: lines in loop don't execute when program is run

    Where is NumInFam initialized? (Or what is it?)

    Setup Quick watch on several variables in the loop and see what value they get assigned.

  5. #5
    Registered User
    Join Date
    08-08-2012
    Location
    Chicago IL
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: lines in loop don't execute when program is run

    Quote Originally Posted by Tinbendr View Post
    Where is NumInFam initialized? (Or what is it?)

    Setup Quick watch on several variables in the loop and see what value they get assigned.
    NumInFam is initialized at the top of the module and it is a public variable. Here is the code:
    Public InputForm As New FrmInputs
    Public ChooseCellForm As New FrmChooseCell
    Public CutAddress As Range
    Public NumberOfEngines As Integer
    Public PasteAddress As Range
    Public NumInFam As Integer
    
    
    Sub StartForms()
        'Shows first input form
        InputForm.Show
    
        'Decides how many times loop runs
        NumberOfEngines = 0
        If InputForm.txtEngMod1.Value <> "" Then
            NumberOfEngines = NumberOfEngines + 1
        End If
        If InputForm.txtEngMod2.Value <> "" Then
            NumberOfEngines = NumberOfEngines + 1
        End If
        If InputForm.txtEngMod3.Value <> "" Then
            NumberOfEngines = NumberOfEngines + 1
        End If
        If InputForm.txtEngMod1.Value = "" And InputForm.txtEngMod2.Value = "" And InputForm.txtEngMod3.Value = "" Then
            NumberOfEngines = Row
        End If
    
        
        NumInFam = 0
        Dim i As Integer
        For i = 1 To [A65536].End(xlUp).Row
            If Cells(1, i).Value = InputForm.cboPwrFam.Value Then
                NumInFam = NumInFam + 1
            End If
        Next i
    
    End Sub
    Also, I've been watching the variables as I step through the program, and EngSizeEnter gets assigned the right value and dbEngSize doesn't even get assigned.

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: lines in loop don't execute when program is run

    what is the value of NumInFam when you run the code? if it's 0 then the inner loop won't run at all.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  7. #7
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: lines in loop don't execute when program is run

    In the Sub FindRowToInsert, and just before For Row = i To (i + (NumInFam - 1)), add

    Debug.Print i + (NumInFam - 1)
    and see what it is returning. If it's less than 1, then that is your problem.

  8. #8
    Registered User
    Join Date
    08-08-2012
    Location
    Chicago IL
    MS-Off Ver
    Excel 2010
    Posts
    15

    Re: lines in loop don't execute when program is run

    Tinbendr, Thank you so much for this useful tool!!! and JosephP, yes it's returning zero, so I guess I'll have to figure that one out. Thanks so much for your help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1