Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Need help with an if statement


I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated

--
SuitedAce
-----------------------------------------------------------------------
SuitedAces's Profile: http://www.excelforum.com/member.php...fo&userid=3584
View this thread: http://www.excelforum.com/showthread.php?threadid=55641

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Need help with an if statement


If rng.Value = "" Then

or

If IsEmpty(rng.Value) Then

or

If Len(rng.value) = 0 Then

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"SuitedAces" wrote
in message ...

I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated.


--
SuitedAces
------------------------------------------------------------------------
SuitedAces's Profile:

http://www.excelforum.com/member.php...o&userid=35840
View this thread: http://www.excelforum.com/showthread...hreadid=556412



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Need help with an if statement

Bob,
This topic interests me, so I'll barge in. This does not seem to
work:

Sub RangeStatus2()
Dim z As Range
Set z = [a1:b5]
If IsEmpty(z.Value) Then
MsgBox "empty"
Else
MsgBox "not empty"
End If
End Sub

What did I do wrong?
James


Bob Phillips wrote:
If rng.Value = "" Then

or

If IsEmpty(rng.Value) Then

or

If Len(rng.value) = 0 Then

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"SuitedAces" wrote
in message ...

I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated.


--
SuitedAces
------------------------------------------------------------------------
SuitedAces's Profile:

http://www.excelforum.com/member.php...o&userid=35840
View this thread: http://www.excelforum.com/showthread...hreadid=556412


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Need help with an if statement

Zone,

IsEmpty is used to test if a variable is has been initialized. This does
work with a single cell value as this is a simple variant, but not a range
of cells, as this is a 2 dimensional array of values. Even though all of the
elements of the array are empty, the array is not.

You need to test a range differently, either loop through them, or use a
worksheet function

Sub RangeStatus2()
Dim z As Range
Set z = Range("A1:B5")
If Application.CountIf(z, "<") = 0 Then
MsgBox "empty"
Else
MsgBox "not empty"
End If
End Sub


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Zone" wrote in message
oups.com...
Bob,
This topic interests me, so I'll barge in. This does not seem to
work:

Sub RangeStatus2()
Dim z As Range
Set z = [a1:b5]
If IsEmpty(z.Value) Then
MsgBox "empty"
Else
MsgBox "not empty"
End If
End Sub

What did I do wrong?
James


Bob Phillips wrote:
If rng.Value = "" Then

or

If IsEmpty(rng.Value) Then

or

If Len(rng.value) = 0 Then

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"SuitedAces"

wrote
in message

...

I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated.


--
SuitedAces


------------------------------------------------------------------------
SuitedAces's Profile:

http://www.excelforum.com/member.php...o&userid=35840
View this thread:

http://www.excelforum.com/showthread...hreadid=556412




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Need help with an if statement

The range is multiple cells?

dim myRng as range
if application.counta(myrng) = 0 then
msgbox "all the cells in that range are empty
end if

if myRng is a single cell:
if isempty(myrng.value) then
msgbox "It's empty"
end if

SuitedAces wrote:

I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated.

--
SuitedAces
------------------------------------------------------------------------
SuitedAces's Profile: http://www.excelforum.com/member.php...o&userid=35840
View this thread: http://www.excelforum.com/showthread...hreadid=556412


--

Dave Peterson


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 269
Default Need help with an if statement

Suited, Here is another way of doing this. May not be the most
elegant, but it does tell you how many cells aren't empty. James

Sub RangeStatus()
Dim z As Range, c As Range, ct As Long
Set z = [a1:b5] 'set range as needed
ct = 0
For Each c In z
If VarType(c) < vbEmpty Then ct = ct + 1
Next c
MsgBox ct
'if ct<0 then one or more cells is not empty
End Sub



SuitedAces wrote:
I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated.


--
SuitedAces
------------------------------------------------------------------------
SuitedAces's Profile: http://www.excelforum.com/member.php...o&userid=35840
View this thread: http://www.excelforum.com/showthread...hreadid=556412


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Need help with an if statement

See my later response.

--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"Zone" wrote in message
oups.com...
Suited, Here is another way of doing this. May not be the most
elegant, but it does tell you how many cells aren't empty. James

Sub RangeStatus()
Dim z As Range, c As Range, ct As Long
Set z = [a1:b5] 'set range as needed
ct = 0
For Each c In z
If VarType(c) < vbEmpty Then ct = ct + 1
Next c
MsgBox ct
'if ct<0 then one or more cells is not empty
End Sub



SuitedAces wrote:
I have Dim a variable as a range.

I am trying to write an IF statement based on that range being empty,
and having no success.

Any help is appreciated.


--
SuitedAces
------------------------------------------------------------------------
SuitedAces's Profile:

http://www.excelforum.com/member.php...o&userid=35840
View this thread:

http://www.excelforum.com/showthread...hreadid=556412



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF statement inside a SUMIF statement.... or alternative method Sungibungi Excel Worksheet Functions 3 December 4th 09 06:22 PM
Reconcile Bank statement & Credit card statement & accounting data Bklynhyc Excel Worksheet Functions 0 October 7th 09 09:07 PM
Embedding an OR statement in an IF statement efficiently Chatnoir11 Excel Discussion (Misc queries) 4 February 2nd 09 08:12 PM
Sum if statement with a left statement Eric D Excel Discussion (Misc queries) 4 July 23rd 08 05:31 PM
appending and IF statement to an existing IF statement spence Excel Worksheet Functions 1 February 28th 06 11:00 PM


All times are GMT +1. The time now is 06:42 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"