If a range is equal to null, skip code and exit, otherwise run code?

ljubo_gr

Active Member
Joined
Dec 6, 2014
Messages
251
Office Version
  1. 2013
Platform
  1. Windows
Hi everybody!
After a while I'm still not into VBA, please, I need to check some range, if it's sum is greater then zero then run code normal, if it's equal zero then skip bunch of code to Exit Sub.

Code:
Sub Macro1_OCISTI_PODATKE_SA_IZVJESTAJA()
'
' Macro1_OCISTI_PODATKE_SA_IZVJESTAJA Macro
'
'
Dim MSG As String, ANS As Variant
MSG = "  SIGURNO  ŽELITE  OČISTITI  PODATKE ?"
ANS = MsgBox(MSG, vbExclamation + vbOKCancel + vbDefaultButton2, "UPOZORENJE !!!")
Select Case ANS
Case vbOK
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
        On Error GoTo GRESKA2
            Sheets("Izvještaj_SVE").Select
            Columns("B:B").Select
'Ovaj kod kopira izvješće na Izvještaj_SVE list.
            Dim R As Long
            If WorksheetFunction.CountA(Cells(2, 2).EntireColumn) > 18241 Then
                Rows("18242:18242").Select
                Range(Selection, Selection.End(xlDown)).Select
                Selection.Delete Shift:=xlUp
                Range("A1").Select
                Rows("2:48").Select
                Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                Range("A2").Select

This is the beginning of code, where to insert if statement?

Thanks so much !
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Hi:

You insert the IF Statement prior to the code you may or may not like to run. If you are looking for it to skip the entire code then place the IF Statement in the beginning maybe before the line "On Error GoTo GRESKA2"

You'd just use something like this (code taken from: :If cell is empty then exit sub)
Code:
    If Sheet1.Cells(15, 2) = "" Then 
        MsgBox "B15 must contain a value" 
        Sheet1.Activate 
        Cells(15, 2).Select 
    Else: Exit Sub 
    End If

If you post your entire code it would be easier to tell you where to put your IF Statement. NOTE: BEFORE YOU EXIT THE SUB MAKE SURE TO TURN ScreenUpdating etc back on.

Good Luck,
Mark
 
Upvote 0
Maybe like this:

If WorksheetFunction.Sum(Worksheets("Sheet1").Range("A1:B5"))>0 Then

my code here with all that Application.screen updating etc.

Else Exit Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,183
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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