0

Given the code below. I'm trying to end the for loop as soon as the condition that the value TOTALE ORE has been found. I'm having a hard time figuring this out. Any help is much appreciated.

For Each source_total_hours In source_month.Resize(50, 2)
            If source_total_hours.Value = "TOTALE ORE" Then 'Cerca TOTALE ORE
                For Each source_hours In source_total_hours.Offset(0, 3).Resize(, 50)
                    If IsNumeric(source_hours) Then 'Aggiungi a lista ore se valore è numerico
                    hours_list.Add (source_hours)
                    End If
                Next source_hours
            End If
Next source_total_hours

Don't know where to end the loop with an Exit for or something similar.

Edit: I rephrase. I would like it to end after completing the cycle, but if if TOTALE ORE HAS BEEN FOUND don't look for another one and do the whole cycle again.

Edit 2: Nothing changes if I add after inner loop.

For Each source_total_hours In source_month.Resize(50, 2)
            If source_total_hours.Value = "TOTALE ORE" Then 'Cerca TOTALE ORE
                For Each source_hours In source_total_hours.Offset(0, 3).Resize(, 50)
                    If IsNumeric(source_hours) Then 'Aggiungi a lista ore se valore è numerico
                    hours_list.Add (source_hours)
                    End If
                Next source_hours
                Exit For
            End If
        Next source_total_hours
  • Does this answer your question? [Excel VBA - exit for loop](https://stackoverflow.com/questions/9414969/excel-vba-exit-for-loop) – FunThomas Jun 30 '22 at 14:59
  • Unfortunately not. Adding it like that doesn't go to the next For Loop that is the source_hours loop. – Simon Riccio Jun 30 '22 at 15:03
  • Put it after the inner loop? – Warcupine Jun 30 '22 at 15:11
  • @Warcupine nothing changes if I add it after inner loop. Shown the update in original post Edit 2. – Simon Riccio Jun 30 '22 at 15:23
  • Then I have no idea what you are trying to accomplish here. – Warcupine Jun 30 '22 at 15:27
  • As posted in your edit, the `Exit For` will exit the loop `For Each source_total_hours In source_month.Resize(50, 2)` If it *doesn't* do that then there's something else we're not seeing here. – Tim Williams Jun 30 '22 at 15:48
  • Just curious, why do the outer loop while what you want is that the inner loop will be run only once and only after the outer loop has the looped cell value "TOTALE ORE" for the first time ? Say at the first iteration of the outer loop, the source_total_hours value is not "TOTALE ORE" ---> then it will skip your inner loop and directly go to the next source_total_hours iteration. It keep on doing like that until say at the 10th iteration, the source_total_hours value is "TOTALE ORE", so then it do your inner loop. And after this you want the outer loop stop, am I correct ? Please CMIIW. – karma Jun 30 '22 at 15:53
  • Anyway .... if you want to have the inner loop to run only one time using the first occurence of "TOTALE ORE" cell address within source_month.Resize(50, 2) as source_total_hours variable, then maybe you may want to try using find method. For example `Set rg = source_month.Resize(50, 2)` ... next line `Set source_total_hours = rg.Find("TOTALE ORE", after:=Range(Split(rg.Address, ":")(1)))` ... then do your `For Each source_hours In source_total_hours.Offset(0, 3).Resize(, 50)` – karma Jun 30 '22 at 17:01

0 Answers0