Where to put the Errorhandler

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
380
Office Version
  1. 365
Platform
  1. Windows
Good afternoon,

Got a macro and in the macro I would like to stop it running if there is an error in the IF statements below. Is that possible?

Thank you for your help.



VBA Code:
  Action = True
    If Worksheets("Boekingen").Cells(2, 2) = Empty Then
        MsgBox ("Export folder niet gevuld")
        Action = False
    End If
    Action = True
     If Worksheets("Boekingen").Cells(6, 11) = Empty Then
        MsgBox ("'Van locatie' niet gevuld")
        Action = False
    End If
    Action = True
     If Worksheets("Boekingen").Cells(6, 12) = Empty Then
        MsgBox ("'Aantal' niet gevuld")
        Action = False
    End If
    Action = True
     If Worksheets("Boekingen").Cells(6, 13) = Empty Then
        MsgBox ("Verplaats' niet gevuld")
        Action = False
    End If
    
       Action = True
    If Worksheets("Boekingen").Cells(1, 2) = Empty Then
        MsgBox ("Datum niet gevuld")
        Action = False
    End If
    

   
      ' Medewerker = Worksheets("Boekingen").Cells(4, 2)
'   ----------------------------------------------------------------------------------------
'   Step 2: Write records for Boekingen
'   ----------------------------------------------------------------------------------------
    Itemrow = 6
    While Worksheets("Boekingen").Cells(Itemrow, 1) <> Empty And Action
        Datum = Worksheets("Boekingen").Cells(1, 2)
        Artikelcode = Worksheets("Boekingen").Cells(Itemrow, 1)
        Bedrag = Worksheets("Boekingen").Cells(Itemrow, 3)
        Vrrdrek = Worksheets("Boekingen").Cells(Itemrow, 6)
        Magazijn = Worksheets("Boekingen").Cells(Itemrow, 7)
        LocatieVan = Worksheets("Boekingen").Cells(Itemrow, 11)
        LocatieNaar = Worksheets("Boekingen").Cells(Itemrow, 12)
        Aantal = Worksheets("Boekingen").Cells(Itemrow, 13)
'
        Call WriteRecord(a)
        Itemrow = Itemrow + 1
    Wend
'   ----------------------------------------------------------------------------------------
'   Step 3: End file
'   ----------------------------------------------------------------------------------------
    If Action = True Then
        
        a.WriteLine ("</GLEntries>")
    End If
    a.WriteLine ("</eExact>")
    a.Close
End Sub

Private Sub WriteRecord(ByVal a As Object)
    a.WriteLine ("<GLEntry entry=""""" & " status=""E"">")
    a.WriteLine ("<Description>" & Omschrijving & "</Description>")
    a.WriteLine ("<Date>" & Datum & "</Date>")
    a.WriteLine ("<Journal code=""" & Dagboek & """ type=""M""/>")
    
    a.WriteLine ("<FinEntryLine number=""" & Itemrow - 11 & """ type= ""N"" subtype=""G"">")
    a.WriteLine ("<Date>" & Datum & "</Date>")
    a.WriteLine ("<GLAccount code=""" & Vrrdrek & """/>")
    a.WriteLine ("<Costcenter code=""" & KPL & """/>")
    a.WriteLine ("<Description>" & Omschrijving & "</Description>")
    a.WriteLine ("<Item code=""" & Artikelcode & """/>")
    a.WriteLine ("<Warehouse code=""" & Magazijn & """/>")
    a.WriteLine ("<WarehouseLocation code=""" & LocatieNaar & """/>")
    a.WriteLine ("<Quantity>" & Replace(Aantal, ",", ".") & "</Quantity>")
    a.WriteLine ("<Amount><Currency code=""EUR""/><Debit>" & Replace(Bedrag * Aantal, ",", ".") & "</Debit></Amount>")
    a.WriteLine ("</FinEntryLine>")
    a.WriteLine ("<FinEntryLine number=""" & Itemrow - 11 & """ type= ""N"" subtype=""G"">")
    a.WriteLine ("<Date>" & Datum & "</Date>")
    a.WriteLine ("<GLAccount code=""" & Vrrdrek & """/>")
    a.WriteLine ("<Costcenter code=""" & KPL & """/>")
    a.WriteLine ("<Description>" & Omschrijving & "</Description>")
    a.WriteLine ("<Item code=""" & Artikelcode & """/>")
    a.WriteLine ("<Warehouse code=""" & Magazijn & """/>")
    a.WriteLine ("<WarehouseLocation code=""" & LocatieVan & """/>")
    a.WriteLine ("<Quantity>" & Replace(Aantal * -1, ",", ".") & "</Quantity>")
    a.WriteLine ("<Amount><Currency code=""EUR""/><Debit>" & Replace(Bedrag * Aantal * -1, ",", ".") & "</Debit></Amount>")
    a.WriteLine ("</FinEntryLine>")
    
    a.WriteLine ("</GLEntry>")
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
By error you mean a runtime error that Excel raises? Then after your Dim statements put
On Error GoTo errHandler (or make up your own line label for the GoTo)
Before your End Sub line put
VBA Code:
exitHere:
'reset any altered any application settings & Set created objects = Nothing here
Exit Sub

errHandler:
Msgbox "Error " & Err.Number & ": " & Err.Description 'or omit this if you don't want to see an error message
Resume exitHere
If you mean exit if a cell is Empty, that's not an error. To end the sub in that case, use Exit Sub before End If.
 
Upvote 0
Solution
By error you mean a runtime error that Excel raises? Then after your Dim statements put
On Error GoTo errHandler (or make up your own line label for the GoTo)
Before your End Sub line put
VBA Code:
exitHere:
'reset any altered any application settings & Set created objects = Nothing here
Exit Sub

errHandler:
Msgbox "Error " & Err.Number & ": " & Err.Description 'or omit this if you don't want to see an error message
Resume exitHere
If you mean exit if a cell is Empty, that's not an error. To end the sub in that case, use Exit Sub before End If.
Good morning,
Thank you that is exactly what I needed.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,329
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top