Variable Value Being Returned as 0 (zero) for empty cells

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,661
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I have this code:

VBA Code:
       . . .      
       For i = 2 To lstrow
            mcval = .Range("J" & i)
            If IsNumeric(mcval) Then
                .Range("W" & i) = "YES"
            Else
                .Range("W" & i) = "NO"
            End If
        Next i
        . . .

It acquires the value of mcval from a cell in column J (Ji). It checks to see if it's numeric and simply reacts to whether it is or isn't. Normally, column J will either hold dates (numbers), or be empty.
When I run this code, mcval = 0 for empty cells, and thus is numeric. This isn't wrong of course, but I'm trying to identify the empty cells. I suspect it might have something to do with having declared mcval as long.
What must I change to ensure that the value for mcval on empty cells is null vs zero?
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
No need to set it equal to a variable.
Try this to check to see if it is a number AND not empty:
VBA Code:
       For i = 2 To lstrow
            If IsNumeric(.Range("J" & i)) And (.Range("J" & i) <>"") Then
                .Range("W" & i) = "YES"
            Else
                .Range("W" & i) = "NO"
            End If
        Next i
 
Upvote 0
Solution
I'd test for empty cells specifically.
 
Upvote 0
I suspect it might have something to do with having declared mcval as long.
And yes and no. :)
If you take an empty cell to a variable of type Long, the variable actually takes the value 0. But the IsNumeric(Empty) test also returns 0 (Empty, is the value returned by an empty cell).
What must I change to ensure that the value for mcval on empty cells is null vs zero?
My suggestion:
VBA Code:
    ...
    
    Dim v As Variant
    Dim vResult As Variant

    v = .Range("J2:" & lstrow).Value
    ReDim vResult(1 To UBound(v), 1 To 1)

    For i = 1 To UBound(v)
        If Len(v(i, 1)) <> 0 Then
            If IsNumeric(v(i, 1)) Then
                vResult(i, 1) = "YES"
            Else
                vResult(i, 1) = "NO"
            End If
        Else
            vResult(i, 1) = "NO"
        End If
    Next i

    .Range("W2").Resize(UBound(vResult)).Value = vResult

    ...
It is not worth creating a compound condition on one line because both (or more) conditions must be tested. When the compound condition is written as a separate one, the code has a chance to execute faster because not all conditions need to be tested.
On the other hand, it is worthwhile to take the data used in the loop from an array rather than a cell, since reading from an array is many times faster.

Artik
 
Upvote 0
Thank you all for your suggestions. Providing different methods to accomplish the same result is very educational and I find that invaluable. That alone is valued more than the solution itself.
 
Upvote 0
You are welcome.
Glad we were able to help!
 
Upvote 0

Forum statistics

Threads
1,226,074
Messages
6,188,729
Members
453,495
Latest member
Pippie4trnc

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