Announcement

Collapse
No announcement yet.

On Error Stop!?!

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • On Error Stop!?!



    How do you stop the code or terminate it if there's an error? replace the msgbox with something like "Command Terminated" and then just stop, don't do the rest of the code!?!?!?!?

    Thank you all ahead for your help...

  • #2
    Re: On Error Stop!?!

    Vikxcel,

    Well I'm not a big error trapper ( so just me ).


    However, something like this should work.

    Code:
    Sub AnySub()
    '
    On Error Goto Terminate
    '
    '
    '
    ' Your Code
    '
    '
    '
    
    Exit sub
    Terminate:
    Msgbox "You've had a fatal error"
    end 
    
    End sub
    Last edited by iwrk4dedpr; October 20th, 2005, 08:46.
    Regards,
    Barry

    My Favorite New Thing:
    Dynamic Named Ranges



    The alternative for
    "Press Any Key To Continue."

    and we all have one we'd like to use it on

    1. Cross Posting Etiquette
    2. Are You Here To Learn: What Have You Tried?
    3. Your Best Resource to begin learning VBA: RECORD A MACRO . . . Then Study It!

    Comment


    • #3
      Re: On Error Stop!?!

      Originally posted by iwrk4dedpr
      Vikxcel,

      Well I'm not a big error trapper ( so just me ).


      However, something like this should work.

      Code:
      Sub AnySub()
      '
      On Error Goto Terminate
      '
      '
      '
      ' Your Code
      '
      '
      '
      
      Exit sub
      Terminate:
      Msgbox "You've had a fatal error"
      end 
      
      End sub
      Ok, so here's a question, why does it show that MSGBOX no matter what, even if there's no error! this is annoying me! Here's my cheap code, go at it:

      Code:
      Sub Import_Data_2()
      '
      ' Import_Data_2 Macro
      ' Macro recorded 2/9/2005 by Viktor Kulikovskiy
      '
      
      
      ' Select Last row
      Dim lRow As Integer
      Dim myRng As Range
      lRow = Range("A" & Rows.Count).End(xlUp).row
      Set myRng = Range("A" & lRow + 1)
      
      'Import the csv file, error is if the file is not there then need to say "please get drawigns"
         ActiveSheet.Unprotect
          With ActiveSheet.QueryTables.Add(Connection:="TEXT;C:\program files\transmitt-xls\combined.csv", _
              Destination:=myRng)
               On Error GoTo Msg
              .name = "combined_1"
              .FieldNames = True
              .RowNumbers = False
              .FillAdjacentFormulas = False
              .PreserveFormatting = True
              .RefreshOnFileOpen = False
              .RefreshStyle = xlInsertDeleteCells
              .SavePassword = False
              .SaveData = True
              .AdjustColumnWidth = True
              .RefreshPeriod = 0
              .TextFilePromptOnRefresh = False
              .TextFilePlatform = 437
              .TextFileStartRow = 1
              .TextFileParseType = xlDelimited
              .TextFileTextQualifier = xlTextQualifierSingleQuote
              .TextFileConsecutiveDelimiter = False
              .TextFileTabDelimiter = False
              .TextFileSemicolonDelimiter = False
              .TextFileCommaDelimiter = True
              .TextFileSpaceDelimiter = False
              .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
              .TextFileTrailingMinusNumbers = True
              .Refresh BackgroundQuery:=False
      End With
       
      'resize the columns
      
          Columns("A:A").EntireColumn.AutoFit
          Columns("B:B").EntireColumn.AutoFit
          Columns("C:C").EntireColumn.AutoFit
          Columns("D:D").EntireColumn.AutoFit
          Columns("E:E").EntireColumn.AutoFit
          Columns("F:F").EntireColumn.AutoFit
          
      'run a batch to delete that file
          Program = "c:\program files\transmitt-xls\erase.bat"
          TaskID = Shell(Program, vbNormalNoFocus)
          
      'protect the sheet
          ActiveSheet.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True _
              , AllowSorting:=True, AllowFiltering:=True
                     
      
      
      'find the last cell in row A and delete the one right before that
      Do
          If IsEmpty(ActiveCell) = False Then
          ActiveCell.Offset(1, 0).Select
          End If
          Loop Until IsEmpty(ActiveCell) = True
          
          ActiveCell.Offset(-1, 0).ClearContents
      
      
      'here's that monkey, it keeps coming up, error or not.... :(
      Msg: MsgBox "Get Drawings"
                            
          
      End Sub

      Comment


      • #4
        Re: On Error Stop!?!

        NEvermind, sorry guys, forgot an END in there....
        OOOPS....

        Comment


        • #5


          Re: On Error Stop!?!

          Vikxcel,

          Let's stop and look.


          Code:
               
              ActiveCell.Offset(-1, 0).ClearContents 
               
               
               'here's that monkey, it keeps coming up, error or not.... :(
              Msg: MsgBox "Get Drawings" 
               
               
          End Sub

          In between that clearcontents and the Msgbox you don't exit the routine.

          If you don't place an Exit Sub prior to the MSG: label it continues on. So add an "Exit Sub" on the line just after the clearcontents command.
          Regards,
          Barry

          My Favorite New Thing:
          Dynamic Named Ranges



          The alternative for
          "Press Any Key To Continue."

          and we all have one we'd like to use it on

          1. Cross Posting Etiquette
          2. Are You Here To Learn: What Have You Tried?
          3. Your Best Resource to begin learning VBA: RECORD A MACRO . . . Then Study It!

          Comment

          Working...
          X