+ Reply to Thread
Results 1 to 10 of 10

Macro calculating incorrectly.. what can cause this?

  1. #1
    Forum Contributor
    Join Date
    02-03-2005
    Location
    Chicago, IL
    Posts
    101

    Macro calculating incorrectly.. what can cause this?

    TIA for any help offered!

    I have a macro in which I am looking to see if a range is blank (you may have seen it in my other posts to this site). In the macro, I have tried the following arguements (I have tested all rng(s) and variables in the macro and know they work):

    For Each cell In rng1
    If IsEmpty(cell) Then
    Set rng3 = Range(Cells(cell.Row, 4), .Cells(cell.Row, icol).Offset(0, -2))
    nonblank = 0
    With rng3
    On Error Resume Next
    nonblank = .SpecialCells(xlCellTypeFormulas).Cells.Count
    nonblank = .SpecialCells(xlCellTypeConstants).Cells.Count
    End With
    If nonblank > 0 Then
    cell.Interior.ColorIndex = 6
    End If
    End If
    Next cell

    and I have tried...

    For Each cell In rng1
    If IsEmpty(cell) Then
    Set rng3 = Range(Cells(cell.Row, 4), .Cells(cell.Row, icol).Offset(0, -2))
    nonblank = 0
    With rng3
    On Error Resume Next
    nonblank = Evaluate("CountA(rng3)")

    End With
    If nonblank <> 0 Then
    cell.Interior.ColorIndex = 6
    End If
    End If
    Next cell

    Both of these statements seem to work only some of the time. There are instances where rng3 holds no data but the macro calculates that data is present. I have checked the cells in question using the functions COUNTA and ISBLANK, both of which indicate the cell is blank.

    Does it have something to do with the statements I used?

  2. #2
    Carim
    Guest

    Re: Macro calculating incorrectly.. what can cause this?

    Hi Celt,

    Depending on how users operate ... some use the space bar to delete
    cell contents ...
    To be on the safe side, you 'd be better off using:
    If Len(yourcell) = 0 Then ...

    HTH
    Cheers
    Carim


  3. #3
    Forum Contributor
    Join Date
    02-03-2005
    Location
    Chicago, IL
    Posts
    101

    Thumbs up

    Hi Carim.

    Hope all is well with you.

    So deleting with the spacebar leaves something in the cell that the macro picks up as a "constant" or Data? Good to know.

    Thanks very much!!

  4. #4
    Tom Ogilvy
    Guest

    RE: Macro calculating incorrectly.. what can cause this?

    nonblank = Evaluate("CountA(rng3)")

    would need to be

    nonblank = Evaluate("CountA(" & rng3.Address & ")")

    or
    nonblank = Application.CountA(rng3)

    --
    Regards,
    Tom Ogilvy



    "Celt" wrote:

    >
    > TIA for any help offered!
    >
    > I have a macro in which I am looking to see if a range is blank (you
    > may have seen it in my other posts to this site). In the macro, I have
    > tried the following arguements (I have tested all rng(s) and variables
    > in the macro and know they work):
    >
    > For Each cell In rng1
    > If IsEmpty(cell) Then
    > Set rng3 = Range(Cells(cell.Row, 4), .Cells(cell.Row, icol).Offset(0,
    > -2))
    > nonblank = 0
    > With rng3
    > On Error Resume Next
    > nonblank = .SpecialCells(xlCellTypeFormulas).Cells.Count
    > nonblank = .SpecialCells(xlCellTypeConstants).Cells.Count
    > End With
    > If nonblank > 0 Then
    > cell.Interior.ColorIndex = 6
    > End If
    > End If
    > Next cell
    >
    > and I have tried...
    >
    > For Each cell In rng1
    > If IsEmpty(cell) Then
    > Set rng3 = Range(Cells(cell.Row, 4), .Cells(cell.Row, icol).Offset(0,
    > -2))
    > nonblank = 0
    > With rng3
    > On Error Resume Next
    > nonblank = Evaluate("CountA(rng3)")
    >
    > End With
    > If nonblank <> 0 Then
    > cell.Interior.ColorIndex = 6
    > End If
    > End If
    > Next cell
    >
    > Both of these statements seem to work only some of the time. There are
    > instances where rng3 holds no data but the macro calculates that data is
    > present. I have checked the cells in question using the functions
    > COUNTA and ISBLANK, both of which indicate the cell is blank.
    >
    > Does it have something to do with the statements I used?
    >
    >
    > --
    > Celt
    > ------------------------------------------------------------------------
    > Celt's Profile: http://www.excelforum.com/member.php...o&userid=19413
    > View this thread: http://www.excelforum.com/showthread...hreadid=530987
    >
    >


  5. #5
    Forum Contributor
    Join Date
    02-03-2005
    Location
    Chicago, IL
    Posts
    101
    Got it.

    Thanks Tom!!!

  6. #6
    Forum Contributor
    Join Date
    02-03-2005
    Location
    Chicago, IL
    Posts
    101

    Question

    Hi Tom,

    Hoping you could help me with this one too. I'm trying to piggyback off of the guidance you gave me earlier.

    All my ranges work. I have Dim'd "cell" as Range.

    For Each cell In rng1
    If IsNumeric(cell) = True Then
    nodecimal = Evaluate("Trunc(" & cell.Address & ")-(" & cell.Address & ")")
    If nodecimal <> 0 Then
    cell.Interior.ColorIndex = 6

    End If
    End If
    Next cell
    End With

    When I debug and use "run to cursor" I see that the variable cell=Nothing. I think I am using this variable inappropriately or have it defined incorrectly for what I want to do. Can you point me in the right direction to the correct method? Thanks in advance for any help given.
    Last edited by Celt; 04-07-2006 at 11:11 PM.

  7. #7
    Carim
    Guest

    Re: Macro calculating incorrectly.. what can cause this?

    Hi Celt,

    Hope everything is going fine, also with you ...

    Regarding your problem, I believe the problem is with the way you have
    declared the variable nodecimal ...
    with Dim nodecimal As Double ... it should be fine ...
    In addition, my personaI preference for the formula would be :
    nodecimal = cell.Value - Int(cell.Value)

    HTH
    Cheers
    Carim


  8. #8
    Forum Contributor
    Join Date
    02-03-2005
    Location
    Chicago, IL
    Posts
    101
    Thanks very much Carim!!

    I think I understand it now. I'll give your coding a try.
    Last edited by Celt; 04-08-2006 at 11:50 AM.

  9. #9
    Carim
    Guest

    Re: Macro calculating incorrectly.. what can cause this?

    You are welcome ...

    Carim


  10. #10
    Carim
    Guest

    Re: Macro calculating incorrectly.. what can cause this?

    You are welcome ...

    Carim


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1