Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
ann ann is offline
external usenet poster
 
Posts: 210
Default Search & extract from multiple workbooks

I have over 20 workbooks (each with several worksheets), that I would like to
search for the character string "03-", and when it is found, copy the entire
row it is in into a separate workbook. None of the workbooks are exactly the
same, so I need to scan the entire book, including each sheet.

If I need to run a search and create a new worksheet in each file to capture
the results, then pull each new worksheet into a new workbook, I can do that.

I am using Excel 2003, not good with VBA creation but familiar with general
programming syntax. Can anyone help?

Thanks,
Ann
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Search & extract from multiple workbooks

Sub copyData()
dim sh as Worksheet, sh1 as Worksheet
Dim rng as Range, rng1 as Range
dim fName as String
Dim sAddr as String
Dim bk as Workbook

' sheet to copy data to:
set sh1 = thisworkbook.Worksheets("Sheet1")

fName = Dir("C:\MyFolder\*.xls")
do while fName < ""
set bk = Workbooks.Open("C:\Myfolder\" & fName)
for each sh in bk.Worksheets
set rng = sh.Cells.Find("03-*", Lookin:=xlValues, _
Lookat:=xlWhole,MatchCase:=False)
if not rng is nothing then
sAddr = rng.Address
do
set rng1 =sh1.Cells(rows.count,1).End(xlup)(2)
rng.EntireRow.copy Destination:=rng1
if isempty(rng1) then rng1.Value = "ZZZZXXX"
set rng = sh.Cells.FindNext(rng)
Loop rng.Address < sAddr
end if
Next sh
fName = Dir()
Loop
End Sub


Would be a start.
Code is untested and may contain typos, but should be close.
Assumes all the workbooks are in a single directory and you want to process
all workbooks in that directory.



--
Regards,
Tom Ogilvy


"Ann" wrote in message
...
I have over 20 workbooks (each with several worksheets), that I would like

to
search for the character string "03-", and when it is found, copy the

entire
row it is in into a separate workbook. None of the workbooks are exactly

the
same, so I need to scan the entire book, including each sheet.

If I need to run a search and create a new worksheet in each file to

capture
the results, then pull each new worksheet into a new workbook, I can do

that.

I am using Excel 2003, not good with VBA creation but familiar with

general
programming syntax. Can anyone help?

Thanks,
Ann



  #3   Report Post  
Posted to microsoft.public.excel.programming
ann ann is offline
external usenet poster
 
Posts: 210
Default Search & extract from multiple workbooks

I am getting a "Compile Error: syntax error" for this line:
Loop rng.Address < sAddr

Otherwise, should work ok, all workbooks are in the same directory and I
want to search all of them.

Any help? What am I missing?

Thanks,
Ann
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Search & extract from multiple workbooks

Loop while rng.Address < sAddr

--
Regards,
Tom Ogilvy

"Ann" wrote in message
...
I am getting a "Compile Error: syntax error" for this line:
Loop rng.Address < sAddr

Otherwise, should work ok, all workbooks are in the same directory and I
want to search all of them.

Any help? What am I missing?

Thanks,
Ann



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Search & extract from multiple workbooks

You probably don't want all the workbooks open at the end either, so I added
a line:

Sub copyData()
Dim sh As Worksheet, sh1 As Worksheet
Dim rng As Range, rng1 As Range
Dim fName As String
Dim sAddr As String
Dim bk As Workbook

' sheet to copy data to:
Set sh1 = ThisWorkbook.Worksheets("Sheet1")

fName = Dir("C:\MyFolder\*.xls")
Do While fName < ""
Set bk = Workbooks.Open("C:\Myfolder\" & fName)
For Each sh In bk.Worksheets
Set rng = sh.Cells.Find("03-*", LookIn:=xlValues, _
Lookat:=xlWhole, MatchCase:=False)
If Not rng Is Nothing Then
sAddr = rng.Address
Do
Set rng1 = sh1.Cells(Rows.Count, 1).End(xlUp)(2)
rng.EntireRow.Copy Destination:=rng1
If IsEmpty(rng1) Then rng1.Value = "ZZZZXXX"
Set rng = sh.Cells.FindNext(rng)
Loop While rng.Address < sAddr
End If
Next sh
' added line to close workbook
bk.Close Savechanges:=False
fName = Dir()
Loop
End Sub

--
Regards,
Tom Ogilvy

"Tom Ogilvy" wrote in message
...
Loop while rng.Address < sAddr

--
Regards,
Tom Ogilvy

"Ann" wrote in message
...
I am getting a "Compile Error: syntax error" for this line:
Loop rng.Address < sAddr

Otherwise, should work ok, all workbooks are in the same directory and I
want to search all of them.

Any help? What am I missing?

Thanks,
Ann







  #6   Report Post  
Posted to microsoft.public.excel.programming
ann ann is offline
external usenet poster
 
Posts: 210
Default Search & extract from multiple workbooks

Works like a charm!

One other thing, more convenience than anything, is there a way to have it
add the originating file name to the row of data it returns?

Such as:
"FileOne.xls" "03-54345"

Thanks so much!

"Tom Ogilvy" wrote:

Loop while rng.Address < sAddr

--
Regards,
Tom Ogilvy

"Ann" wrote in message
...
I am getting a "Compile Error: syntax error" for this line:
Loop rng.Address < sAddr

Otherwise, should work ok, all workbooks are in the same directory and I
want to search all of them.

Any help? What am I missing?

Thanks,
Ann




  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Search & extract from multiple workbooks

Sub copyData()
Dim sh As Worksheet, sh1 As Worksheet
Dim rng As Range, rng1 As Range
Dim fName As String
Dim sAddr As String
Dim bk As Workbook

' sheet to copy data to:
Set sh1 = ThisWorkbook.Worksheets("Sheet1")

fName = Dir("C:\MyFolder\*.xls")
Do While fName < ""
Set bk = Workbooks.Open("C:\Myfolder\" & fName)
For Each sh In bk.Worksheets
Set rng = sh.Cells.Find("03-*", LookIn:=xlValues, _
Lookat:=xlWhole, MatchCase:=False)
If Not rng Is Nothing Then
sAddr = rng.Address
Do
Set rng1 = sh1.Cells(Rows.Count, 1).End(xlUp)(2)
rng.EntireRow.Copy Destination:=rng1
rng1.Insert Shift:=xlShifttoRight
' uncomment to get sheet name too
rng1.Offset(0,-1).Value = bk.Name ' & "!" & rng.Parent.Name
Set rng = sh.Cells.FindNext(rng)
Loop While rng.Address < sAddr
End If
Next sh
' added line to close workbook
bk.Close Savechanges:=False
fName = Dir()
Loop
End Sub

--
Regards,
Tom Ogilvy



"Ann" wrote in message
...
Works like a charm!

One other thing, more convenience than anything, is there a way to have it
add the originating file name to the row of data it returns?

Such as:
"FileOne.xls" "03-54345"

Thanks so much!

"Tom Ogilvy" wrote:

Loop while rng.Address < sAddr

--
Regards,
Tom Ogilvy

"Ann" wrote in message
...
I am getting a "Compile Error: syntax error" for this line:
Loop rng.Address < sAddr

Otherwise, should work ok, all workbooks are in the same directory and

I
want to search all of them.

Any help? What am I missing?

Thanks,
Ann






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
Search multiple workbooks upsidedown_pw[_2_] Excel Worksheet Functions 3 August 30th 09 12:16 AM
Search across multiple workbooks Ed K Excel Discussion (Misc queries) 2 August 2nd 09 01:30 PM
extract data from multiple workbooks heather Excel Discussion (Misc queries) 1 December 20th 07 08:05 AM
extract from multiple workbooks in a folder smonsmo Excel Discussion (Misc queries) 8 August 19th 07 09:57 PM
How do I extract cells from multiple workbooks Trevor Excel Discussion (Misc queries) 1 November 25th 04 10:59 PM


All times are GMT +1. The time now is 09:33 AM.

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

About Us

"It's about Microsoft Excel"