I am quite new in VBA and I have an Excel sheet in which some macros are running. In one of the tabs, users can input data and by pressing the command button on the sheet they can get the answer. Now I want to enter data via a userform. I've successfully created an userform and it can perfectly read/write data from the Excel sheet. The problem I am facing now is that as soon as I enter the data in the userform, the command button (which is connected to a macro) gives me "Division by zero" error, and it kinda ruins the Excel file and I need to replace it with the backup file I have! I can't understand why entering data via the userform causes this problem.
When I enter data on the Excel sheet (not via userform) everything is fine!!! This is the part I'm getting the error:
Function ATn2(x As Variant, Y As Variant) As Double
If x = 0 Then
If Y = 0 Then
ATn2 = 1 / 0
ElseIf Y > 0 Then
ATn2 = Pi / 2
Else
ATn2 = -Pi / 2
End If
ElseIf x > 0 Then
If Y = 0 Then
ATn2 = 0
Else
ATn2 = Atn(Y / x)
End If
Else
If Y = 0 Then
ATn2 = Pi
Else
ATn2 = (Pi - Atn(Abs(Y) / Abs(x))) * Sgn(Y)
End If
End If
End Function
And of course I can see that the error is coming from ATn2 = 1 / 0 :)
1/0cannot work in any circumstance. The code worked only when y <> 0. Anyhow, try changingATn2(x As Variant, Y As Variant)withATn2(x As Double, Y As Double). Not for solving the division by zero error... - FaneDuruAnd of course I can see that the error is coming from ATn2 = 1 / 0- so you are aware that it happens because you are callingATn2(0, 0). So don't do that. - GSerg