Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Countif in multiple books

I have a folder with 518 files, I need to count the number
of times "KKL" occurs in a range called "rng1" and then
return the value to cell A2 in a new book.

Is this possible?

I don't fancy having to do it all manually.

Thanks in advance.

Gareth
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Countif in multiple books

Something like:

sub CountKKL()
Dim sName as String
Dim wkbk as Workbook
sname = Dir("C:\Myfolder\*.xls")
do while sName < ""
set wkbk = workbooks.open(sName)
cnt = cnt + Application.Countif(rng,"KKL")
wkbk.Close Savechanges:=False
sName = dir()
Loop
workbooks.Add
ActiveWorkbook.Worksheets(1).Range("A2").Value = cnt
End Sub

Make sure the workbook containing the code is not in your directory and all
of the 518 files are not open in excel.

--
Regards,
Tom Ogilvy



"Gareth" wrote in message
...
I have a folder with 518 files, I need to count the number
of times "KKL" occurs in a range called "rng1" and then
return the value to cell A2 in a new book.

Is this possible?

I don't fancy having to do it all manually.

Thanks in advance.

Gareth



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Countif in multiple books

Tom

I'm getting run-time error 1004, "myfile.xls could not be
found". but it is there!

Any suggestions?

Gareth
-----Original Message-----
Something like:

sub CountKKL()
Dim sName as String
Dim wkbk as Workbook
sname = Dir("C:\Myfolder\*.xls")
do while sName < ""
set wkbk = workbooks.open(sName)
cnt = cnt + Application.Countif(rng,"KKL")
wkbk.Close Savechanges:=False
sName = dir()
Loop
workbooks.Add
ActiveWorkbook.Worksheets(1).Range("A2").Value = cnt
End Sub

Make sure the workbook containing the code is not in your

directory and all
of the 518 files are not open in excel.

--
Regards,
Tom Ogilvy



"Gareth" wrote in

message
...
I have a folder with 518 files, I need to count the

number
of times "KKL" occurs in a range called "rng1" and then
return the value to cell A2 in a new book.

Is this possible?

I don't fancy having to do it all manually.

Thanks in advance.

Gareth



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Countif in multiple books

My oversight. Change

set wkbk = workbooks.open(sName)

to

set wkbk = workbooks.open("C:\MyFolder\" & sName)


--
Regards,
Tom Ogilvy


"Gareth" wrote in message
...
Tom

I'm getting run-time error 1004, "myfile.xls could not be
found". but it is there!

Any suggestions?

Gareth
-----Original Message-----
Something like:

sub CountKKL()
Dim sName as String
Dim wkbk as Workbook
sname = Dir("C:\Myfolder\*.xls")
do while sName < ""
set wkbk = workbooks.open(sName)
cnt = cnt + Application.Countif(rng,"KKL")
wkbk.Close Savechanges:=False
sName = dir()
Loop
workbooks.Add
ActiveWorkbook.Worksheets(1).Range("A2").Value = cnt
End Sub

Make sure the workbook containing the code is not in your

directory and all
of the 518 files are not open in excel.

--
Regards,
Tom Ogilvy



"Gareth" wrote in

message
...
I have a folder with 518 files, I need to count the

number
of times "KKL" occurs in a range called "rng1" and then
return the value to cell A2 in a new book.

Is this possible?

I don't fancy having to do it all manually.

Thanks in advance.

Gareth



.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 37
Default Countif in multiple books

Tom

Sorry to be a pain but the code is stopping at this line:

cnt = cnt + Application.Countif(rng,"KKL")

Run-time error 13 - Type mismatch.

Any ideas?

-----Original Message-----
My oversight. Change

set wkbk = workbooks.open(sName)

to

set wkbk = workbooks.open("C:\MyFolder\" & sName)


--
Regards,
Tom Ogilvy


"Gareth" wrote in

message
...
Tom

I'm getting run-time error 1004, "myfile.xls could not

be
found". but it is there!

Any suggestions?

Gareth
-----Original Message-----
Something like:

sub CountKKL()
Dim sName as String
Dim wkbk as Workbook
sname = Dir("C:\Myfolder\*.xls")
do while sName < ""
set wkbk = workbooks.open(sName)
cnt = cnt + Application.Countif(rng,"KKL")
wkbk.Close Savechanges:=False
sName = dir()
Loop
workbooks.Add
ActiveWorkbook.Worksheets(1).Range("A2").Value = cnt
End Sub

Make sure the workbook containing the code is not in

your
directory and all
of the 518 files are not open in excel.

--
Regards,
Tom Ogilvy



"Gareth" wrote in

message
...
I have a folder with 518 files, I need to count the

number
of times "KKL" occurs in a range called "rng1" and

then
return the value to cell A2 in a new book.

Is this possible?

I don't fancy having to do it all manually.

Thanks in advance.

Gareth


.



.



  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Countif in multiple books

Assuming you have a name defined as rng1 in each file, then it would be

sub CountKKL()
Dim sName as String
Dim wkbk as Workbook
sname = Dir("C:\Myfolder\*.xls")
do while sName < ""
set wkbk = workbooks.open(sName)
cnt = cnt + Application.Countif(Range("rng1"),"KKL")
wkbk.Close Savechanges:=False
sName = dir()
Loop
workbooks.Add
ActiveWorkbook.Worksheets(1).Range("A2").Value = cnt
End Sub

I actually had intended to do

sub CountKKL()
Dim sName as String
Dim wkbk as Workbook
Dim rng as Range
sname = Dir("C:\Myfolder\*.xls")
do while sName < ""
set wkbk = workbooks.open(sName)
On Error Resume Next
set rng = wkbk.names("rng1").RefersToRange
On Error goto 0
if not rng is nothing then
cnt = cnt + Application.Countif( _
rng,"KKL")
End if
wkbk.Close Savechanges:=False
sName = dir()
Loop
workbooks.Add
ActiveWorkbook.Worksheets(1).Range("A2").Value = cnt
End Sub

but must have gotten distracted.

--
Regards,
Tom Ogilvy



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Viewing multiple books David Excel Discussion (Misc queries) 0 June 4th 09 12:53 AM
multiple sheets 2 work books capt c Excel Worksheet Functions 0 April 8th 09 03:03 PM
Large Project with multiple books bobb Excel Discussion (Misc queries) 0 November 1st 06 06:37 PM
Viewing multiple books cdyork Excel Discussion (Misc queries) 0 August 3rd 05 01:08 PM
Combine multiple books into one list Steve Mackay Excel Programming 4 October 29th 04 12:40 AM


All times are GMT +1. The time now is 05:23 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"