WorksheetFunction.Countif to Other Workbook

razzandy

Active Member
Joined
Jun 26, 2002
Messages
400
Office Version
  1. 2007
Platform
  1. Windows
Hi Guys Struggling on this one. I need to use WorksheetFunction.Countif in VBA to access info in another workbook but I cant get it to work.

My Code is:

t = Application.WorksheetFunction.CountIf('C:\My Directory\[Stock.xlsm]Main Stock'!$B$B, "<>")


Thanks in Advance

;-)
 
Last edited:

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
First, make sure that the workbook Stock.xlsm is opened. Then try...

Code:
t = Application.WorksheetFunction.CountIf(Workbooks("Stock.xlsm").Worksheets("Main Stock").Range("B:B"), "<>")

Hope this helps!
 
Upvote 0
Thanks Domenic....

I've tried your code with the addition of the file directory in front of the workbook name because I needed it to access a closed workbook but it doesn't work :-( I should have been clearer in my original post, sorry!

I know I could just enter the countif formula with VBA directly in a cell together with the full file path but I wanted a more streamlined way of doing it without writing to the active workbook.

Any more ideas?
 
Last edited:
Upvote 0
Maybe it's not ideal, but I would simply open the workbook, get the value needed, and then close it. I would also set ScreenUpdating to False at the start of the macro so that you can't see the workbook being opened and closed. All that would be done in the background. And then set ScreenUpdating back to false at the end.

Otherwise, go with your original idea and enter the formula in a cell, etc. ;)
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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