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]
 

Some videos you may like

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Scott T

Well-known Member
Joined
Dec 14, 2016
Messages
2,615
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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.

<b>Excel 2010</b><table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)"><colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th><th>B</th><th>C</th><th>D</th></tr></thead><tbody><tr ><td style="color: rgb(22,17,32);text-align: center;">2</td><td style=";">Walkthrough</td><td style=";">Not Started</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">3</td><td style=";">Walkthrough</td><td style=";">Not Started</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">4</td><td style=";">interim</td><td style=";">Not Started</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">5</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">6</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: rgb(22,17,32);text-align: center;">7</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;">1</td></tr></tbody></table><p style="width:4.8em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid rgb(187,187,187);border-top:none;text-align: center;background-color: rgb(218,231,245);color: rgb(22,17,32)">Sheet1</p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: rgb(255,255,255)" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: rgb(255,255,255);border-collapse: collapse; border-color: rgb(187,187,187)"><thead><tr style=" background-color: rgb(218,231,245);color: rgb(22,17,32)"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: rgb(218,231,245);color: rgb(22,17,32)">D7</th><td style="text-align:left">=COUNTIFS(<font color="Blue">A2:A4,"interim",B2:B4,"not Started"</font>)</td></tr></tbody></table></td></tr></table><br />
 

Youngdand

Board Regular
Joined
Sep 29, 2017
Messages
120
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")


 

frank_AL

Active Member
Joined
Oct 30, 2015
Messages
439
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")
 

Watch MrExcel Video

Forum statistics

Threads
1,109,140
Messages
5,527,059
Members
409,742
Latest member
setam

This Week's Hot Topics

Top