IT-Academy

Ošetrenie chýb Excel

Apr 20th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. Sub TestovaciaProcedura()
  3.  
  4.     Dim i As Integer
  5.  
  6.     Dim x As Integer
  7.  
  8.     For i = -1 To 1
  9.  
  10.         x = 3 / i
  11.  
  12.     Next i
  13.  
  14. End Sub
  15.  
  16.  
  17. Sub OsetrenieChyby1()
  18.  
  19.     Dim i As Integer
  20.     Dim x As Integer
  21.  
  22.     'pri chybe skoè na dalsi riadok
  23.  
  24.     On Error Resume Next
  25.     For i = -1 To 1
  26.  
  27.         x = 3 / i
  28.         Debug.Print (x)
  29.  
  30.     Next i
  31. End Sub
  32.  
  33. Sub OsetreniChyby2()
  34.  
  35.     Dim i As Integer
  36.  
  37.     Dim x As Integer
  38.  
  39.     'pri chybe skoè na dalši riadok
  40.  
  41.     On Error Resume Next
  42.  
  43.     For i = -1 To 1
  44.  
  45.         x = 3 / i
  46.  
  47.         'zaregistroval objekt Err chybu?
  48.  
  49.         If Err <> 0 Then
  50.  
  51.             'evidencia èísla a popisu chyby
  52.            'do okna Immediate(anglicky)
  53.  
  54.             Debug.Print Err.Number & vbTab & Err.Description
  55.             Debug.Print Err.Source; 7; Err.HelpFile
  56.  
  57.             'reset
  58.  
  59.             Err.Clear
  60.  
  61.         End If
  62.  
  63.     Next i
  64.  
  65. End Sub
  66.  
  67. Sub OsetrenieChyby3()
  68.  
  69.     Dim i As Integer
  70.  
  71.     Dim x As Integer
  72.  
  73.     'pri chybe skoè na riadok Chyba
  74.  
  75.     On Error GoTo Chyba
  76.  
  77.     For i = -1 To 1
  78.  
  79.         x = 3 / i
  80.         MsgBox x
  81.  
  82.     Next i
  83.  
  84. Chyba:
  85.     MsgBox "Nastala chyba..." & vbNewLine & _
  86.            "Chyba cislo: " & Err.Number & vbNewLine & _
  87.            "Popis chyby: " & Err.Description
  88.  
  89. End Sub
  90.  
  91. Sub OsetrenieChyby4()
  92.  
  93.     Dim i As Integer
  94.  
  95.     Dim x As Integer
  96.  
  97.     'pri chybe skoè na riadok Chyba
  98.  
  99.     On Error GoTo Chyba
  100.  
  101.     For i = -1 To 1
  102.  
  103.         x = 3 / i
  104.  
  105.     Next i
  106.  
  107.     Exit Sub
  108.  
  109. Chyba:
  110.  
  111.     'chyba delenia nulou
  112.  
  113.     '    If Err.Number = 11 Then
  114.    '        MsgBox (Err.Description)
  115.    '    End If
  116.  
  117.     Select Case Err.Number
  118.     Case 11
  119.         Debug.Print "Chyba delenia 0 " & vbTab & Err.Description
  120.     Case 12
  121.         Debug.Print "Chyba ... " & vbTab & Err.Description
  122.     Case 500 To 1000
  123.         Debug.Print "Aplikacna chyba, alebo chyba definici obj " & vbTab & Err.Description
  124.     Case Else
  125.         Debug.Print "Neviem co si za chybu " & vbTab & Err.Description
  126.     End Select
  127.  
  128.  
  129.     'opravit promennej
  130.  
  131.     i = i + 1
  132.  
  133.     'návrat na riadok, ktorý vyvolal chybu
  134.  
  135.     Resume
  136.  
  137.  
  138.  
  139.     'alternativa bez priameho ošetrenia promennej
  140.  
  141.     'Resume Next
  142.  
  143. End Sub
  144.  
  145. Sub piata()
  146.  
  147.     On Error GoTo Log
  148.  
  149.     Beep
  150.  
  151.  
  152. Log:
  153.     Debug.Print Err.Number & " " & Err.Description
  154.     GoTo Koniec
  155.  
  156. Koniec:
  157.     Exit Sub
  158.  
  159. End Sub
  160.  
  161.  
  162. Sub siesta()
  163.  
  164.     Beep
  165.     MsgBox "ad", vbOKOnly, "sdf"
  166.  
  167.  
  168. d_Exit:
  169.     Exit Sub
  170.  
  171. d_Err:
  172.     MsgBox Error$
  173.     Resume d_Exit
  174.  
  175.  
  176. End Sub
  177.  
  178.  
  179. Public Sub SomeMacro()
  180.     'store the initial "calculation" state
  181.    Dim xlCalc As XlCalculation
  182.     xlCalc = Application.Calculation
  183.  
  184.     On Error GoTo Handler
  185.     SpeedUp True
  186.    
  187.     'code here ...
  188.    i = 1 / 0
  189. CleanExit:
  190.     SpeedUp False, xlCalc
  191.     Exit Sub
  192. Handler:
  193.     'handle error
  194.    Resume CleanExit
  195. End Sub
  196.  
  197. Sub testujemeHodnoty(Optional ByVal a As Integer, Optional ByVal b As String)
  198.  
  199.     Debug.Print "Si v prvej, a: " & a & ", b: " & b
  200.     'testujemeHodnoty
  201.  
  202. End Sub
  203.  
  204. Sub testujemeHodnoty2()
  205.  
  206.     Call testujemeHodnoty
  207.     Call testujemeHodnoty(5)
  208.     Call testujemeHodnoty(5, "Adam")
  209. End Sub
  210.  
  211. 'Function vypisAhojSvet()
  212. '
  213. '    MsgBox "Ahoj svet"
  214. '    vypisAhojSvet = "Ahoj svet"
  215. '
  216. 'End Function
  217.  
  218.  
  219. Function vypisAhojSvet(Optional ByVal a)
  220.  
  221.     MsgBox "Ahoj svet2"
  222.     vypisAhojSvet = "Ahoj svet2"
  223.  
  224. End Function
Advertisement
Add Comment
Please, Sign In to add comment