MagicSteve
New Member
- Joined
- Dec 30, 2024
- Messages
- 8
- Office Version
- 365
- Platform
- 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?
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