Countif Error

gaurav.inani

New Member
Joined
Jul 17, 2010
Messages
8
Hi,

I have created a new excel macro enabled workbook in Excel 2007.

I have entered the following in Sheet1
A1 = Name
A2 = Mike
A3 = Jullian
A4 = Peter
A5 = Rusell

and in Column B, as

B1 = Say
B2 = Hi
B3 = Hello
B4 = Hi
B5 = Hi

in my sheet2 i have
A1=Hi
A2=Hello

and then I have activated sheet2 and trying to enter formula through VBA

Sub test()
dim r as range
Sheet(ActiveSheet.Index - 1).Select
Set r = Columns("B:B")
Sheet(ActiveSheet.Index + 1).Select
Range("B1").Select
Activecell.Formula = "=Countif(r,A1)"
End sub

(This I am doing because the user does not know which sheet is activated and hence in Sheet2 the forumla have to return the value 3)

But it is generating error, can anyone help me on this topic.




 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

gaurav.inani

New Member
Joined
Jul 17, 2010
Messages
8
and when i am trying this formula this is working fine

activecell.formulaR1C1 = "=COUNTIF(Sheet1!B1:B5,Sheet2!A1)"

But what if I dont know from which sheet I have to select range then what I have to do.......
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
It's pretty unclear what you want to do here.

What would you expect r to be replaced with when the formula was on the worksheet?

Why are you using ActiveSheet and Index for the worksheets?
 

gaurav.inani

New Member
Joined
Jul 17, 2010
Messages
8
What i am trying to do is
in Sheet2 it will reply 3 when i enter the formula of countif based on the range selected from Sheet1 i.e. B1:B5
But my main concern is what if the user rename the sheet Sheet1 to Hi then formula doesn't work.....
As i am new i dont know how to use code tags
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

Well you can easily get a worksheet's name using it's Name property.

You could then incorporate that into the formula.
Rich (BB code):
Range("B1").Formula = "=Countif('" & Sheets(ActiveSheet.Index - 1).Name & "'!B:B,A1)"
 

gaurav.inani

New Member
Joined
Jul 17, 2010
Messages
8
Hey thanks a lot Norie, its working now.......... :)

But will u please explain me the logic of the same

"=Countif('" & Sheets(ActiveSheet.Index - 1).Name & "'!B:B,A1)"

As i am new to VBE i wanted to know the logic.....

specially

'
&

And one more thing :

I want to insert the nos. of rows in Sheet3 based on the value in Cell B1 in Sheet2
But again I dont know the name of Sheet3 so do i use the same format or the different one???
 
Last edited:

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,343
Office Version
  1. 365
Platform
  1. Windows
Well the ' is nothing to do with VBA, it's needed for the formula on the worksheet to deal with things like spaces.
 

gaurav.inani

New Member
Joined
Jul 17, 2010
Messages
8
And one more thing :

I want to insert the nos. of rows in Sheet3 based on the value in Cell B1 in Sheet2
But again I dont know the name of Sheet3 so do i use the same format or the different one???

Here in this case 3.
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,163,700
Messages
5,833,213
Members
430,197
Latest member
edeibold

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
Top