-1

On compilation, I get a syntax error on the line On Error GoTo ErrorHandler "Near Column 15" which is the beginning of GoTo. I also tried adding a colon to the end of the line (On Error GoTo ErrorHandler:) as I've seen some people do, and I get the same error. What is the syntax error that I'm missing?

Subroutine:

Sub HandleAnError()
    On Error GoTo ErrorHandler
    
    Dim X
    X = 1/0
    
    Exit Sub
    
ErrorHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
End Sub
Community
  • 1
  • 1
ejderuby
  • 710
  • 5
  • 21
  • Okay, I tried it and I get the same error. Updated the question. – ejderuby Aug 14 '19 at 14:18
  • 1
    Copy and pasting your code into a general module in excel runs fine. Where are you running it? – Harassed Dad Aug 14 '19 at 14:31
  • It's compiling and running in an independent program. I don't have the details of what compiler it's using, but that may be the problem. – ejderuby Aug 14 '19 at 14:37
  • Are you sure you are talking about VBA? Not VB.Net? Does this code live within Excel or Word or similar? – FunThomas Aug 14 '19 at 14:41
  • Hmm, I thought it was VBA. It's not within a Microsoft program. I guess I didn't know the distinction. Pretty new to this language, and was trying to edit something that already existed. Try Catch hasn't worked either.. -_- – ejderuby Aug 14 '19 at 14:47
  • If you even don't know which language you are using, how can we help you. Where do you edit your code? How do you call it? Where does the compiler error come from? – FunThomas Aug 14 '19 at 14:52
  • @FunThomas I edit in Notepad++, I call it as `HandleAnError()` on one line, and the compiler error comes from a janky application my work uses that compiles and runs the source code every time the application starts. – ejderuby Aug 14 '19 at 14:56
  • Or interpreted rather than compiled. This is new terminology to me. I believe it's interpreted based on how the application runs and can be changed while it's running. VBA is interpreted, so this is VBA to my knowledge. – ejderuby Aug 14 '19 at 15:03
  • 2
    Probably you deal with VB.Script (something really different than VBA!!!). Have a look to https://stackoverflow.com/a/157785/7599798 – FunThomas Aug 14 '19 at 15:15
  • 1
    @FunThomas You're right, this fixed my problem. Thank you for your help – ejderuby Aug 14 '19 at 15:46

1 Answers1

-1
Dim X = 1/0  

should be

Dim X

X=1/0
Harassed Dad
  • 4,669
  • 1
  • 10
  • 12
  • I get the same error when I fixed that. – ejderuby Aug 14 '19 at 14:19
  • 1
    Don't think this should have been downvoted. It solves the problem as posted - OP has since moved the goalposts. – SJR Aug 14 '19 at 14:50
  • @SJR That wasn't the error that I was asking about. If I fixed the error I was asking about, then I would get an error about this, but I never got an error about this... – ejderuby Aug 14 '19 at 14:53
  • If you put your code into the VBE with this amendment the code runs as expected. Perhaps you should close the question and start again. – SJR Aug 14 '19 at 14:56
  • This answer didn't answer my question as it was when it was first opened. It was a simple syntax error unrelated to my question. – ejderuby Aug 14 '19 at 15:07