Exit Sub if No Contents Exist

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
946
Office Version
  1. 2016
Platform
  1. Windows
Hello All

Code:
    Sheets("Template").Select
    Range("A9").Select
    If Selection <> contents Then
    Exit Sub
    End If

I'm trying to not run the sub, if Range A9 in the Template worksheet is blank (no data exist). If A9 has nothing in it, the Template work sheet simply remains the same. If A9 does have data in it, then the rest of the program runs.
Thanks for the help
excel 2013
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Another option is
Code:
    If Len(Sheets("Template").Range("A9").Value) = 0 Then Exit Sub
 
Upvote 0
Thanks to both,
Both lines worked, I choose Dave's...Sorry Fluff
 
Upvote 0
Glad we could help & thanks for the feedback.

Also there's nothing to apologise for. All that counts is that you have a working solution. :)
 
Upvote 0
Code:
If Sheets("Template").Range("G9") = vbNullString Then
Exit Sub
End If

Here is my code above. It Works because there is either a number in G9, or it is blank. When G9 is blank, the program exits, as it should.

My Question: Why if I change the Range to G9:F100 then it no longer works? even when there is a number in G9 and most of the cells in this range?
Thanks for the help
excel 2013
 
Last edited:
Upvote 0
Hi,

I can't explain the why for you but it must have something to do with how a range of more than 1 cell is seen.
As I understand it, to manipulate/check strings in a range you must look at each cell individually.

Code:
Set rng = Sheets("Template").Range("G9:F100")
 
For Each Cell In rng
If Cell.Value = vbNullString Then
Exit Sub
End If
Next
 
Upvote 0

Forum statistics

Threads
1,215,028
Messages
6,122,753
Members
449,094
Latest member
dsharae57

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