VBA Help - Properly Exit Sub

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hi Group,

Working on a script that is doing a copy/paste type function and looping down a list of text to update a report.

I have the code working fine, but when I come across a blank cell I would like the macro to EXIT SUb, the problem that I am having is that I have code after the Exit sub line that calls other macros if a specific cell has text, My question is, how can I rewrite my code so that the Macro REALLY closes out and exits the sub without doing anymore?

I tried the GOTO approach but I obviously didn't know what I was doing because it didn't work.

My code below:

Code:
Sub Copy_CCodes()
    
    Dim lastRow As Long
    Dim Datastore As Worksheet, FinalDest As Worksheet
    Dim i As Range


    Set Datastore = Sheets("Lookups2")
    Set FinalDest = Sheets("Summary")


    lastRow = Datastore.Cells(Rows.Count, "L").End(xlUp).Row
    Set rng = Datastore.Range("CCodes")
             
  Application.ScreenUpdating = False
  Application.DisplayAlerts = False
  
  Call FindRep
  
  Datastore.Activate
  Range("N2").Select
  
  'Set Do loop to stop when two consecutive empty cells are reached.
      If FinalDest.Range("A9").Value = "" Then
      
      GoTo KillCode
        
        'ActiveCell.Offset(1, 0).Select
            
            Else
      
    'Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
    Do Until IsEmpty(ActiveCell)
         ActiveCell.Copy
              FinalDest.Range("RPTCC").PasteSpecial Paste:=xlValues
                    Calculate
           
    Call SaveSheet
                
         ' Step down 1 rows from present location.
         ActiveCell.Offset(1, 0).Select
      Loop
  
  End If
  
KillCode:
  
  Exit Sub
 
  
Call CountFiles
   
End Sub
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This could be rewritten to avoid the selecting, but for a first pass,

Code:
Sub Copy_CCodes()
  Dim lastRow       As Long
  Dim Datastore     As Worksheet
  Dim FinalDest     As Worksheet

  Set Datastore = Sheets("Lookups2")
  Set FinalDest = Sheets("Summary")

  lastRow = Datastore.Cells(Rows.Count, "L").End(xlUp).Row

  Call FindRep

  Datastore.Activate
  Range("N2").Select

  If FinalDest.Range("A9").Value <> "" Then
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Do Until IsEmpty(ActiveCell)
      FinalDest.Range("RPTCC").Value = ActiveCell.Value
      Calculate

      Call SaveSheet

      ActiveCell.Offset(1, 0).Select
    Loop
    
    Call CountFiles
    Application.ScreenUpdating = True
  End If
End Sub
 
Last edited:
Upvote 0
Thank you for the help on this Shg, I have re-ran with your code and I am still getting the same issue.

This could be rewritten to avoid the selecting, but for a first pass, the reason I need the code to die after a blank cell is reached is due to the call of the Savesheet macro.

Currently, with your updates, the code still calls the SaveSheet Macro even if a blank cell is reached on this line * If FinalDest.Range("A9").Value <> "" Then

I was hoping that when the loop hits a blank cell, it discontinues all activity and does not run any other code after.

Code:
Sub Copy_CCodes()
  Dim lastRow       As Long
  Dim Datastore     As Worksheet
  Dim FinalDest     As Worksheet

  Set Datastore = Sheets("Lookups2")
  Set FinalDest = Sheets("Summary")

  lastRow = Datastore.Cells(Rows.Count, "L").End(xlUp).Row

  Call FindRep

  Datastore.Activate
  Range("N2").Select

  If FinalDest.Range("A9").Value <> "" Then
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Do Until IsEmpty(ActiveCell)
      FinalDest.Range("RPTCC").Value = ActiveCell.Value
      Calculate

      Call SaveSheet

      ActiveCell.Offset(1, 0).Select
    Loop
    
    Call CountFiles
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0
Thanks Mike, tried that already. The problem I am having is that after the code does Exit Sub, it still continues to next argument below which is the Call SaveSheet macro.



Try
Code:
FinalDest.Range("A9").Value = "" Then
    Exit Sub
Else
    ' current code
End If
 
Upvote 0
Step through the code, if it is hitting the Exit Sub line at the right time, keep stepping to see where it's going.
Or replace Exit Sub with END.
 
Upvote 0
Ok, I just figured out what the issue was.

Thank you Shg for the condensed code and Mike for the suggestion about the Stepping thru, I found that I was do the If Statement before I updated the cell that would contain the Blank.

I am however curious Shg, how would I rewrite to not have the SELECT lines of code?

New Code:

Code:
Sub Copy_CCodes()
  
  Dim lastRow       As Long
  Dim Datastore     As Worksheet
  Dim FinalDest     As Worksheet


  Set Datastore = Sheets("Lookups2")
  Set FinalDest = Sheets("Summary")


  lastRow = Datastore.Cells(Rows.Count, "N").End(xlUp).Row


  Call FindRep


  Datastore.Activate
  Range("N2").Select


  If ActiveCell.Value = "" Then
        Exit Sub
            Application.ScreenUpdating = False
                Application.DisplayAlerts = False
                
                Else


    Do Until IsEmpty(ActiveCell)
      FinalDest.Range("RPTCC").Value = ActiveCell.Value
      Calculate


      Call SaveSheet


      ActiveCell.Offset(1, 0).Select
    Loop
    
    Call CountFiles
    Application.ScreenUpdating = True
  End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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