Exit Sub if No Contents Exist

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
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

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
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,224,824
Messages
6,181,186
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