Problem executing VBA code using a button and also getting an output as '0'

ashwin1206

New Member
Joined
Mar 13, 2018
Messages
1
[FONT=&quot]Hello Experts[/FONT]
[FONT=&quot]I am new to vba and very keen in understanding it better. I have written below code around countifs[/FONT]
[FONT=&quot]Issue 1 - When i debug, i do not get any error, instead i get output as '0' even though my input file looks as below. However if i copy paste the keyword 'Walkthrough' in the code and execute it, i would get the correct output i.e 2[/FONT]
[FONT=&quot]Issue 2 - If I assign a button to this macro and run, i would get an output as '0'[/FONT]
[FONT=&quot]Input File[/FONT]
[FONT=&quot]Walkthrough Not Started[/FONT]
[FONT=&quot]Walkthrough Not Started[/FONT]
[FONT=&quot]Actual Code[/FONT]
[FONT=&quot]Sub countif_test()

Dim type_of_test As Long
Dim Walk As Worksheet
Dim source As Worksheet

Set source = Worksheets("Archer Search Report")
Set Walk = Worksheets("SOX_Dashboard_Walkthrough_Stat")

type_of_test = Application.WorksheetFunction.CountIfs(Range("A2:A3"), "Interim", Range("B2:B3"), "Not Started")

Walk.Range("C2") = type_of_test

End Sub[/FONT]

[FONT=&quot]Request you to assist in regard this. Thanks in advance.[/FONT]
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Input File
Walkthrough Not Started
Walkthrough Not Started

I assume this is two columns A and B
what you have posted does not match you countifs requirements as column a does not have interim in it so countifs will return 0.


Excel 2010
ABCD
2WalkthroughNot Started
3WalkthroughNot Started
4interimNot Started
5
6
71
Sheet1
Cell Formulas
RangeFormula
D7=COUNTIFS(A2:A4,"interim",B2:B4,"not Started")
 
Upvote 0
There is nothing in your sample that matches the following criteria:

type_of_test = Application.WorksheetFunction.CountIfs(Range("A2:A3"), "Interim", Range("B2:B3"), "Not Started")


 
Upvote 0
ashwin,

A couple of things to consider. Your code is looking for "Interim" in Range("A2:A3") but you do not have "Interim" in those cells so "0" would be a correct response.
With CountIfs, ALL criteria must be met.

The other thing to consider is the you define 'source' and 'Walk' as Worksheets but do not use those variables when calculating 'type_of_test'. So, if you don't have Worksheet "Archer Search Report" as the active Worksheet the code will only evaluate the current active Worksheet. Consider changing that line of code to the following:
Code:
type_of_test = Application.WorksheetFunction.CountIfs(source.Range("A2:A3"), "Walkthrough", source.Range("B2:B3"), "Not Started")
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,164
Members
453,021
Latest member
Justyna P

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