Unexpected Results With VBA Formula

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
This is a snippit of my module code:
Rich (BB code):
With ws_cd1
            If pbypass = True Then
                cnt_date = Application.WorksheetFunction.CountIf(.Columns(2), n_date)
            Else
                cnt_date = Application.WorksheetFunction.CountIf(.Columns(1), n_date)
            End If
            cnt_rows = Application.WorksheetFunction.Count(.Columns(1))
            rte = cnt_rows - cnt_date
            
            'unprotect core data
            .Unprotect
            With ws_gui1.Range("AP3") 'coredata unlock, fee data is already unlocked
                .Value = "CORE DATA   " & Chr(208) 'unlocked
                .Font.Name = "Arial Narrow"
                .Characters(Len(.Value), 1).Font.Name = "Webdings"
            End With
            
            'filter core data of all dates not equal to queried date
            If pbypass <> True Then
                If .AutoFilterMode Then .AutoFilterMode = False
                .Range("A1").AutoFilter Field:=1, Criteria1:="<>" & n_date
                test = Application.WorksheetFunction.Subtotal(1, .Columns(1))
                'check for data error (count number of filtered rows to difference of queried date from total rows - should be equal)
                If test <> rte Then
                    MsgBox "Data inconsitency encountered: Core_Data Date Redundancy"
                    Stop
                End If

Stepping through the code with pbypass = False,
n_date = 2020-05-23
cnt_date = 24
cnt_rows = 6505 (calculated prior to this snippit)
rte = 6482
Worksheet ws_cd1 is the data source, column 1 holds dates.
The autofilter has has filtered out all instances on 2020-05-23, the visible rows are the other dates.
The problem is with the line in red ... a line of code which I was hoping would given me a count of the number of visible cells. With the 24 rows of data filtered out for 2020-05-23, there should be 6482 visible rows (RTE). I would anticipate that the value of TEST would give me that same value ... the number of rows visible in the data worksheets ws_cd1.
But it's not. It's giving me a value of 44060.9782474545.

Is anyone able to provide a solution to this problem?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Try:
VBA Code:
test = .columns(1).SpecialCells(xlCellVisible).Count
You're using Subtotal to sum the values of the visible cells not count them, hence your result is mixed number, not whole number.
 
Upvote 0
Hi JackDanice, thank you for your suggestion.
I substituted my line with yours but I am now encountering an error:
"Unable to get the SpecialCells property of the Range Class"
 
Upvote 0
Unsure, maybe try:
VBA Code:
Msgbox .Parent.Name 'Confirm what this is
test = .Range("A:A").SpecialCells(xlCellVisible).Count
 
Upvote 0
It needs to be either
VBA Code:
Range("A:A").SpecialCells(xlCellTypeVisible).Count
or
VBA Code:
Range("A:A").SpecialCells(xlVisible).Count
 
Upvote 0
@Ark68, just to also point out that Count is Subtotal(2, Subtotal(1, is Average. The hidden values part in the table only applies to manually hidden rows, if they are hidden by Autofilter then it automatically only includes the visible cells.

Function_num Function_num Function
(includes hidden values)(ignores hidden values)
1101AVERAGE
2102COUNT
3103COUNTA
4104MAX
5105MIN
6106PRODUCT
7107STDEV
8108STDEVP
9109SUM
10110VAR
11111VARP
 
Upvote 0
Thank you all for you help. It's very appreciated to see the interest in wishing to help.
Fluff and JackDanIce, your suggestions would have worked for my immediate needs, but the value still wasn't what was to be expected. I was getting a value of 1048552. As mentioned, this would get me through the first test, but I think it would have messed up more when I need to rely on a more accurate value of TEST further on in my project.

Mark858, I substituted based on your explanation :
Code:
test = Application.WorksheetFunction.Subtotal(1, .Columns(1))
with ...
Code:
test = Application.WorksheetFunction.Subtotal(2, .Columns(1))
With positive results.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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