Question

background:

Im trying to build an mistake (finds spelling mistakes and such) indicator for a huge spreadsheet, mistakes are found in each coulmn depending on a different criteria but in general each part of the code (procedure?) searches for mistakes using a looping if statement if a mistake is found the code goes to ( goto) errorlist which is a procedure that is supposed to tabulate errors and their cells in a new sheet.

Issue:

Goto donot function in the way I imagined the huge if statement at the end doesnt go back in the code, and the code stops running after the first error is tabulated, what can be done to solve this problem as i want the code to go back to the procedure, Im sure that my coding is the problem and the goto statment is able to go up in the code thats why i'll mention the whole lengthy and to be honest messy code so if someone can catch the probelm. please focus on the if statment at the end as im postive it's where the problem lies

code: (only a sample including one procedure of many others, this one checks column L for errors)

Sub errorinsight()

    Sheets("sheet1").Activate
    Dim ucolumn As String



    Dim i As Long, j As Long, r As Range
        ' if your data is in a different column then change L to some other letter(s)
ucolumn = "L" 'pick up contr.


For i = 2 To Range(ucolumn & Rows.Count).End(xlUp).Row
        Set r = Range(ucolumn & i)
        If r = MAXXAM Or r = SGS Then
        GoTo nexti1
        Else

        GoTo errorlist
        End If
complink:
nexti1:
        Next i




GoTo mastercodeend

errorlist:

Sheets.Add.Name = "errorsheet" & Format(Now, "yyyy_mm_dd ss_nn_hh")

    Dim counter As Integer, xerror As Range, yerror As Range

    counter = 1
    Set xerror = Range("a" & counter)
    xerror = ucolumn & i
     Set yerror = Range("b" & counter)
     yerror = r
     counter = counter + 1
      ElseIf ucolumn = "L" Then
     GoTo complink
         Else
         End If
    mastercodeend:
    End Sub
Was it helpful?

Solution

Your best bet is to use some subroutines and/or functions to compartmentalize your code. Using or overusing GoTo statements gets messy, as you observe.

Sub errorinsight()
    Dim i As Long, j As Long, r As Range
    Dim ucolumn As String
    Dim counter As Integer: counter = 0

    Sheets("sheet1").Activate

    'if your data is in a different column then change L to some other letter(s)
    ucolumn = "L" 'pick up contr.

    For i = 2 To Range(ucolumn & Rows.Count).End(xlUp).Row
        Set r = Range(ucolumn & i)
        If Not r = MAXXAM AND Not r = SGS Then
           FlagErrors ucolumn, r, counter + 1
        End If
    Next i
End Sub

Sub FlagErrors(ucolumn as String, i as Long, r as Range, byRef counter as Integer)
    Dim xerror As Range, yerror As Range
    Sheets.Add.Name = "errorsheet" & Format(Now, "yyyy_mm_dd ss_nn_hh")
    Range("A" & counter) = ucolumn & i
    Range("B" & counter) = r
    Sheets("sheet1").Activate

    'I remove this block because it doesn't do anything.
    '  * uColumn is hardcoded as "L" in your procedure above
    '  * samplecodelink is an undefined label in your procedure and will raise an error
    '  * GoTo compLink is unnecessary since this sub will return to the next line in the calling procedure
     'If ucolumn = "A" Then
     'GoTo samplecodelink
     '     ElseIf ucolumn = "L" Then
     'GoTo complink
     '    Else
     '    End If

End Sub
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top