Run Time 1004 Error Unable to get the CountBlank property of the WorksheetFunction class

Bigblue1702

New Member
Joined
Aug 22, 2016
Messages
2
All,

I am trying to create a macro that will work with a button to display either answers to a problem if the test has been completed or "Please finish the test before answers will be displayed".

Here is my current code

Sub Button_1()

Dim Answers As Range

Set Answers = Worksheets("Questions").Range("B21,H21,Q21,Y21,AD21,AL21,AU21,BF21,BQ21,CA21,CH21,CR21,DB21,DJ21,DS21,ED21")


If Application.WorksheetFunction.CountBlank(Answers) > 0 Then

MsgBox "Please finish the test before answers will be displayed"
Else
MsgBox "Test" 'This display will be replaced with specific answers to specific questions....
End If


End Sub

Based on a couple hours of debugging and research I believe it has something to do with how I am defining my objects at the top. I am tech/excel savy and have some lite background in programming on a general engineering-level, but I am no master by any means. Any help would be greatly appreciated. Thanks!
 

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.
Hi and welcome to the MrExcel Message Board.

For reasons known only to Microsoft, you can't use CountBlank like that. It works if the range is contiguous but not otherwise.

So you will need to be a bit more creative. How about:
Code:
Sub Button_1()
    Dim r      As Range
    Dim blank  As Boolean
    
    For Each r In Worksheets("Questions").Range("B21,H21,Q21,Y21,AD21,AL21,AU21,BF21,BQ21,CA21,CH21,CR21,DB21,DJ21,DS21,ED21")
        If IsEmpty(r) Then blank = True
    Next
    
    If blank Then
        MsgBox "Please finish the test before answers will be displayed"
    Else
        MsgBox "Test" 'This display will be replaced with specific answers to specific questions....
    End If
End Sub
That loops round the cells in the range and notes if one of them is blank.


Regards,
 
Upvote 0
I think the following will also work, and doesn't use a loop. (Not that looping through 16 cells will take more than a blink!)

Code:
Sub Button_1()
Dim kount As Long
kount = WorksheetFunction.CountA(Range("B21,H21,Q21,Y21,AD21,AL21,AU21,BF21,BQ21,CA21,CH21,CR21,DB21,DJ21,DS21,ED21"))
If kount < 16 Then
    MsgBox "Please finish the test before answers will be displayed"
Else
    MsgBox "Test" 'This display will be replaced with specific answers to specific questions....
End If
End Sub

Cheers,

tonyyy
 
Upvote 0
RickXL and tonyyy,

Thanks for your alls responses! I was actually able to get this one figured out on my own. Here is my code for anyone curious


Code:
Sub Button_1()


'Define the cells that the answers will go in
Set Answers = Worksheets("Questions").Range("B21,H21,Q21,Y21,AD21,AL21,AU21,BF21,BQ21,CA21,CH21,CR21,DB21,DJ21,DS21,ED21")

'Set a counter to count the number of blank cells
counter = 0
For Each cellObject In Answers
    If cellObject.Value = "" Then counter = counter + 1
    Next
    
'Determine the value of counter. If there are any blank answers, the correct answer will not be displayed.
    If counter > 0 Then
        MsgBox "Please finish the test before answers will be displayed"
    Else
        MsgBox "Test"
    End If

End Sub

Thanks again!
 
Upvote 0
Yet another, slightly different way
Code:
Option Explicit

Sub testComplete()
    Dim Answers As Range

    Set Answers = Range("B21,H21,Q21,Y21,AD21,AL21,AU21,BF21,BQ21,CA21,CH21,CR21,DB21,DJ21,DS21,ED21")
    
    If WorksheetFunction.CountA(Answers) < Answers.Count Then
        MsgBox "Please finish the test before answers will be displayed"
    Else
        MsgBox "Test"
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,523
Messages
6,120,038
Members
448,940
Latest member
mdusw

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