Libreoffice calc basic error but there is no error

Ver 7.5.4.2

I have this code:

Sub GetSet
On Error GoTo line5
line5:
      MsgBox "Error"
      Exit Sub
    Dim iTT As Double
    Dim TVL As Double
    Dim RUN As Double
    Dim TOPANGLE As Double
    Dim oActiveSheet As Object
    Dim oCellRangeByName As Object
    Dim oCellRangeBydeg As Object
    Dim oCellRun As Object
    Dim oCellTopAngle As Object

   
Rem  Gets the Active sheet 
    oActiveSheet = ThisComponent.getCurrentController().getActiveSheet()
Rem Gets access to a cell
    oCellRangeByName = oActiveSheet.getCellRangeByName("G11")
Rem Get Value of cell
    iTT = oCellRangeByName.Value
   oCellRangeBydeg = oActiveSheet.getCellRangeByName("D14")
   DG = oCellRangeBydeg.Value
   TVL = iTT*1 / sin(DG * Pi / 180)
   
   
Rem Get Value of cell
    iTT = oCellRangeByName.Value
    
    
    
    oCelld9 = oActiveSheet.getCellRangeByName("D9")
	oCelld9.setValue(TVL)
	MsgBox(iTT)
	
	'added============================================
	RUN = iTT*1 / tan(DG * Pi / 180)
	
	oCellRun = oActiveSheet.getCellRangeByName("D16")
	oCellRun.setValue(RUN)
	
	'======================
	TOPANGLE = 90 - DG
	oCellTopAngle = oActiveSheet.getCellRangeByName("E10")
	oCellTopAngle.setValue(TOPANGLE)
	
'ErrorHandler:
    'MsgBox "Error " & Err & ": " & Error$ + chr(13) + "At line : " + Erl + chr(13) + Now , 16 ,"an error occurred"
    'Exit Sub
    'MsgBox "All files will be closed",  0,  "Error"
    
	
	'added============================================
End Sub

With no error handler it runs fine.

When I added a

On Error GoTo ErrorHandler

It displayed:

Error 0: 
At line : 0

So as you can see I added:

On Error GoTo line5
line5:
      MsgBox "Error"
      Exit Sub

It goes to that and exits. When the handler is at bottom, it all runs, but still shows the error message.

How can there be an error that early in the sub, it’s as though it catching a non error anyway.

Please hep me figure it out. Note line5: is just a name I picked, I don’t mean the actual line 5.

add exit sub before ErrorHandler

	oCellTopAngle.setValue(TOPANGLE)
	exit sub
ErrorHandler:
1 Like

To explain: your On Error GoTo X does not mark X to only execute on error; if the normal flow comes to your label (be it line5, or ErrorHandler), it continues there normally. If you need to avoid some code in the normal flow, you need to do that explicitly, as @KamilLanda shows. This is shown also in the On Error GoTo help page.

Is it correct to have an Exit Sub before a error handler?

I changed to:

   ' the code above Exit Sub
    Exit Sub
	
ErrorHandler:
    MsgBox "Error " & Err & ": " & Error$ + chr(13) + "At line : " + Erl + chr(13) + Now , 16 ,"an error occurred"

End Sub

And all seems fine now.

Thanks @ mikekaganski, I looked it up, and see that they put an Exit Sub prior to the error handler, thanks both of you.

Your ErrorHandler is normal label, standard part of code, no extraordinary :-). exit sub or exit function is also normal part of code and you can use it at the place you need, for example in loop for .. next etc.

1 Like