Wierd function behavior. Boolean assignment assigns incorrect value

MagicSteve

New Member
Joined
Dec 30, 2024
Messages
8
Office Version
  1. 365
Platform
  1. Windows
Hi friends,
I've been using the CountA function to check for populated cells in a named range. I have some code that has been working correctly for some months.
I moved the code into a Select Case statement to handle 3 cases and it broke.
I have 2 named ranges, PressureAColumn and PressureBColumn which may or may not contain data.
When empty, WorksheetFunction.CountA(sheetIn.[PressureAColumn]) returns zero (and same for PressureBColumn)
So, WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0 is false, because WorksheetFunction.CountA(sheetIn.[PressureAColumn]) is zero
I did a sanity check on both cases assigning temporary booleans. See assignments to dummy1, dummy2 and dummy 3.
All these values are false and the OR evaluates to false, as you'd expect.
In both cases of the Select, the assignment of DataIsEnteredOnSheet (which is the result of false or false) is TRUE!!
????
Anyone have an explanation?
VBA Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function DataIsEnteredOnSheet(sheetIn As Worksheet) As Boolean
  
  Dim dummy1 As Boolean
  Dim dummy2 As Boolean
  Dim dummy3 As Boolean
  
  Select Case sheetIn.Name
    Case "Door Sample"  ' pressures and defects
      dummy1 = WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0
      dummy2 = WorksheetFunction.CountA(sheetIn.[PressureBColumn]) <> 0
      dummy3 = dummy1 Or dummy2
     ' this dummy1 assignment also evaluates to true, and I even put some ( ) around
    '  the CountA calls just in case
      dummy1 = (WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0) _
                Or _
                (WorksheetFunction.CountA([sheetIn.PressureBColumn]) <> 0)

      
      ' DataIsEnteredOnSheet is assigned True ???? !!!!
      DataIsEnteredOnSheet = WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0 _
                                 Or _
                                 WorksheetFunction.CountA([sheetIn.PressureBColumn]) <> 0 _
                                 Or _
                                 sheetIn.[TotalDefects].Value <> 0
    
    Case "Pre-Run"      ' pressures only
      
      dummy1 = WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0
      dummy2 = WorksheetFunction.CountA(sheetIn.[PressureBColumn]) <> 0
      dummy3 = dummy1 Or dummy2
      dummy1 = (WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0) _
                Or _
                (WorksheetFunction.CountA([sheetIn.PressureBColumn]) <> 0)
                
       ' ????? the line below assigns true to DataIsEnteredOnSheet despite all
       ' the values above acting correctly
       ' How is this possible?
      DataIsEnteredOnSheet = WorksheetFunction.CountA(sheetIn.[PressureAColumn]) <> 0 _
                             Or _
                             WorksheetFunction.CountA([sheetIn.PressureBColumn]) <> 0
                             
    Case "PackOut" ' defects only
      DataIsEnteredOnSheet = sheetIn.[TotalDefects].Value <> 0
  End Select
  
End Function
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
What does that mean, "in the wrong place" ?? I am using square brackets to access ranges in about 100 places in the program and they all work just as I expect. What would be "in the right place" ??
 
Upvote 0

Forum statistics

Threads
1,225,743
Messages
6,186,770
Members
453,370
Latest member
juliewar

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