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

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
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.......
 
Upvote 0
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?
 
Upvote 0
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
 
Upvote 0
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)"
 
Upvote 0
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:
Upvote 0
Well the ' is nothing to do with VBA, it's needed for the formula on the worksheet to deal with things like spaces.
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,213,551
Messages
6,114,267
Members
448,558
Latest member
aivin

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