Excel For Loops with Strings help

sgonzalez90

New member
Joined
Jul 7, 2011
Messages
3
Location
Lowell, Massachusetts, United States
Programming Experience
3-5
I want the second main for loop in the 1st main for loop, but I'm having a hard time integrating it so simply. The purpose of this program is to match coordinates and Pass/Fail them within a certain tolerance. I keep getting different results for axes-is other than s, can someone help me please?

VB.NET:
    Private Sub startButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startButton.Click
        On Error GoTo ErrHandler            'If error an error occurs go to the handler (bottom of sub)


        'Kill Excel processes
        If killCheck.Checked = True Then
            Dim pProcess() As Process = System.Diagnostics.Process.GetProcessesByName("notepad")


            For Each p As Process In pProcess
                p.Kill()
            Next
        End If
        '\\\START DECLARATIONS OF VARIABLES////////////////////////////////////////////////


        'Declarations for excel application control
        Dim xlApp As Excel.Application      'Set xlApp as an excel application
        Dim xlBook As Excel.Workbook        'Set xlBook as an excel workbook
        Dim xlSheet1 As Excel.Worksheet     'Set xlSheet1 as an excel worksheet


        'Declarations for excel cell search and load manipulation
        Dim currentRow As Integer                    'For statement iterator (used below)
        Dim position As Integer             'Used for Array position in testArray
        Dim index As Integer                'Used for testArray itteration
        Dim started As Boolean              'Have we searched through the Excel spreadsheet to the starting section yet?
        Dim rindex As Integer               'Used for result1Array itteration
        Dim relVal As Integer               'Converts result1Array to string after doubles are added together




        'Declarations for columns of the Excel document (used for indexing and manipulating data)
        Dim sectionColumn As Excel.Range    'Set sectionColumn as a range of values (values declared below)
        Dim moveColumn As Excel.Range       'Excel column we search for a "move" in.
        Dim resultColumn As Excel.Range     'Excel column we use for result comparisons


        'Declarations for data arrays
        Dim testArray() As String           'Test data array of strings
        Dim result1Array() As String        'Top result data array of strings
        Dim result2Array() As String        'Bottom result data array of strings






        'Declarations for variables in the VB interface that we need to use
        Dim srcStart As String              'Excel spreadsheet's starting test section
        srcStart = section1Text.Text        'Set textbox to variable


        Dim srcEnd As String                'Excel spreadsheet's ending test section
        srcEnd = section2Text.Text          'Set textbox to variable


        'Declarations for test data statistics
        Dim pfColumn As Excel.Range         'Pass/Fail column
        Dim passes As Integer               'Amount of passes
        passes = 0                          'Init passes to 0
        Dim fails As Integer                'Amount of fails
        fails = 0                           'Init fails to 0
    




        '\\\STARTUP CODE: Opens Excel and sets up the columns//////////////////////////////


        xlApp = CreateObject("Excel.Application")           'Use xlApp as Excel Application...


        If showCheck.Checked Then
            xlApp.Visible = True                            'Make spreadsheet visible?, user input checkbox
        End If


        xlBook = xlApp.Workbooks.Open(fileText.Text)        'Open the filepath of XLS doc selected, user defined by fileText
        xlSheet1 = xlBook.Worksheets(sheetText.Text)        'Open the Test Cases worksheet, user defined by sheetText
        sectionColumn = xlSheet1.Range(rangeText.Text)              'Set the search range of entire spreadheet
        moveColumn = xlSheet1.Range(rangeText.Text).Offset(0, 3)    'moveColumn is set to the column we will search for a move command
        resultColumn = xlSheet1.Range(rangeText.Text).Offset(0, 6)  'resultColumn is set to the column we will compare our 2 results from
        pfColumn = xlSheet1.Range(rangeText.Text).Offset(0, 5)      'pfColumn is set to the Pass/Fail column, so we can write in a P/F, accordingly


        '\\\START SEARCH AND COMPARE ALGORITHM////////////////////////////////////////////d()        'Start seaching for the section the user wants to test
        For currentRow = 1 To sectionColumn.Count
 
VB.NET:
MainLoop:

            'Find cell with starting section number and output a message box
            If sectionColumn.Cells(currentRow).Value = srcStart Then




                If (debugCheck.Checked = True) Or (failCheck.Checked = True) Then
                    MsgBox("STARTING AT: " + sectionColumn.Cells(currentRow).Text() + " " + sectionColumn.Cells(currentRow).offset(0, 2).Text() + " Row: " + currentRow.ToString)
                End If
                started = True
            End If




            If sectionColumn.Cells(currentRow).Value = srcEnd Then




                If (debugCheck.Checked = True) Or (failCheck.Checked = True) Then
                    MsgBox("ENDED AT: " + sectionColumn.Cells(currentRow).Text() + " " + sectionColumn.Cells(currentRow).offset(0, 2).Text() + " Row: " + currentRow.ToString)
                End If
                started = False




                If saveCheck.Checked = True Then
                    xlBook.Save()
                End If
                xlBook.Close()
                xlApp.Quit()
                Exit Sub
            End If


            'Start search


            'If we have navigated to the section we want to test...
            If started = True Then


                'Search moveColumn cells for (default) "move"
                If moveColumn.Cells(currentRow).Text().Contains(searchText.Text) Then


                    'Load arrays of data
                    testArray = Split(moveColumn.Cells(currentRow).Text(), " ")
                    result1Array = Split(resultColumn.Cells(currentRow - 1).Text(), " ")
                    result2Array = Split(resultColumn.Cells(currentRow + 1).Text(), " ")


                    For index = 1 To testArray.Length - 1    'Changes data in Array 1




                        If IsNumber(testArray, index) And (testArray(index - 1).ToLower = "rel" Or testArray(index - 1).ToLower = "abs") Then




                            For rindex = 1 To result1Array.Length - 1






                                If (testArray(index - 2).ToLower = result1Array(rindex).ToLower) Then   'Finds moving axes-is






                                    If (testArray(index - 1).ToLower = "rel") Then                      'Cases for seperating rel abs moves
                                        result1Array(rindex + 1) = (Convert.ToDouble(result1Array(rindex + 1))) + (Convert.ToDouble((testArray(index))).ToString)
                                        Exit For






                                    ElseIf (testArray(index - 1).ToLower = "abs") Then
                                        result1Array(rindex + 1) = testArray(index)
                                        Exit For




                                    Else
                                        GoTo AbsRelError                                                'Defaults to AbsRelError without an indication of rel or abs found within the string
                                    End If




                                    If IsNumber(result1Array, position) And IsNumber(result2Array, position) Then
                                        'Fails if the var has a new value, passes if otherwise
                                        MsgBox("First for loop")


                                        If (ArrayCompTol(toDoub(result1Array, position), toDoub(result2Array, position), Convert.ToDouble(tolText.Text)) = True) Then




                                            If debugCheck.Checked = True Then
                                                MsgBox("Pass! " & vbNewLine & "Line: " + currentRow.ToString & vbNewLine & " T1: " + testArray(position - 2) + " " + testArray(position - 1) + " " + testArray(position) & vbNewLine & " R1: " + result1Array(position) + " " + result1Array(position + 1) & vbNewLine & " R2: " + result2Array(position) + " " + result2Array(position + 1))
                                            End If
                                            pfColumn.Cells(currentRow).Value = "P"
                                            passes += 1
                                            currentRow += 1
                                            GoTo MainLoop
                                        Else




                                            If failCheck.Checked = True Then
                                                MsgBox("Fail! " & vbNewLine & "Line: " + currentRow.ToString & vbNewLine & " T1: " + testArray(position - 2) + " " + testArray(position - 1) + " " + testArray(position) & vbNewLine & " R1: " + result1Array(position) + " " + result1Array(position + 1) & vbNewLine & " R2: " + result2Array(position) + " " + result2Array(position + 1))
                                            End If
                                            pfColumn.Cells(currentRow).Value = "F"
                                            fails += 1
                                            currentRow += 1
                                            GoTo MainLoop
                                        End If




                                    End If






                                End If
                            Next rindex
                        End If
                    Next index






                    For position = 1 To result1Array.Length - 1




                        If IsNumber(result1Array, position) And IsNumber(result2Array, position) Then
                            'Fails if the var has a new value, passes if otherwise


                            If (ArrayCompTol(toDoub(result1Array, position), toDoub(result2Array, position), Convert.ToDouble(tolText.Text)) = True) Then




                                If debugCheck.Checked = True Then
                                    MsgBox("Pass! " & vbNewLine & "Line: " + currentRow.ToString & vbNewLine & " T1: " + testArray(position - 2) + " " + testArray(position - 1) + " " + testArray(position) & vbNewLine & " R1: " + result1Array(position) + " " + result1Array(position + 1) & vbNewLine & " R2: " + result2Array(position) + " " + result2Array(position + 1))
                                End If
                                pfColumn.Cells(currentRow).Value = "P"
                                passes += 1
                                currentRow += 1
                                GoTo MainLoop
                            Else




                                If failCheck.Checked = True Then
                                    MsgBox("Fail! " & vbNewLine & "Line: " + currentRow.ToString & vbNewLine & " T1: " + testArray(position - 2) + " " + testArray(position - 1) + " " + testArray(position) & vbNewLine & " R1: " + result1Array(position) + " " + result1Array(position + 1) & vbNewLine & " R2: " + result2Array(position) + " " + result2Array(position + 1))
                                End If
                                pfColumn.Cells(currentRow).Value = "F"
                                fails += 1
                                currentRow += 1
                                GoTo MainLoop
                            End If
                        End If
                    Next position
                End If
            End If
        Next currentRow
        'End Search
        Exit Sub




ErrHandler:  ' Error Handler, shouts a popup error msg


        MsgBox("Critical Data Error" & _
        " Script failed. Go and facepalm.", vbCritical, "Critical Data Processing Error")
        xlBook.Close()
        xlApp.Quit()
        Exit Sub


AbsRelError:
        MsgBox("Critical Data Error" & _
"Error in checking to see if the move was abs/rel.", vbCritical, "Critical Data Processing Error")
        xlBook.Close()
        xlApp.Quit()
        Exit Sub


    End Sub
 
Back
Top