BASIC: How to exit IF - ElseIf - End If

Option Explicit
Sub CheckBlankResultOfFormula
	Dim oDoc, oSheet, oCell As Object
	oDoc = ThisComponent
	oSheet = oDoc.CurrentController.ActiveSheet
	Dim i As Integer	
	Dim t As String
	For i = 0 To 1000
		oCell = oSheet.getCellByPosition(1, i)
		If 		oCell.String = "" Then
'				Next
				GoTo ComeHere
		ElseIf 	oCell.String <> "End" Then
				t = t & oCell.String & Chr(10)
		ElseIf 	oCell.String = "End" Then
				t = t & "End"
		End If
ComeHere:
	Next
	MsgBox t,,"Result"
End Sub

Why can’t Next exit IF - ElseIf - End If ?
0032CheckBlankResultOfFormula.ods (12.7 KB)

LibreOffice:
Version: 7.3.5.2 / LibreOffice Community
Build ID: 30(Build:2)
CPU threads: 4; OS: Linux 5.15; UI render: default; VCL: gtk3
Locale: en-US (en_US.UTF-8); UI: en-US
Ubuntu package version: 1:7.3.5-0ubuntu0.22.04.1
Calc: threaded

OS:
Ubuntu 22.04 LTS

“GoTo” - in 2022 ?!

Try to use “Select Case” if you want to jump over the OTHER conditions.

I suppose it, that you want to decrease the running time of your code. Please compare the two methods embedded your sample file modified by me.

The modified subroutines display the elapsed SystemTicks below the result strings.
0032CheckBlankResultOfFormula_Zizi64.ods (13.3 KB)

  • I am using Select Case instead if the nested IF-s.
  • I call the oCell.String only once.

Because Next ends the ForNext loop; and that is simply how the language is defined. There is an Exit For, but nothing like Continue. You have the GoTo for that, that’s Basic for you :wink: