View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.programming
freddy freddy is offline
external usenet poster
 
Posts: 87
Default Find matching tab names between different workbooks

Thank you for your response but I do not know what do next. Can you help?

"Tom Ogilvy" wrote:

? activesheet.name
Sheet1 (2)
? left(activesheet.name,6)
Sheet1


returned the leftmost 6 characters of the range.

So, perhaps you have clearly stated the problem.

--
Regards,
Tom Ogilvy


"Freddy" wrote in message
...
The following line in your sample does not appear to work:
Set sh1 = bk2.Worksheets(Left(sh.Name, 6))

I am getting returned the entire tab name. Please advise.

"Tom Ogilvy" wrote:

Sub OpenFiles()
Dim sh as Worksheet, sh1 as Worksheet
dim bk as Workbook, bk1 as workbook
Dim bk2 as Workbook
Dim sName as String
Dim sPath as String
Dim bFound as Boolean
for each bk1 in workbooks
if instr(1,bk1.name,"filter.xls",vbTextcompare) then
set bk2 = bk1
exit for
end if
Next
if bk2 is nothing then
Msgbox "Filter workbook not found, exiting"
exit sub
end if

sPath = "C:\Myfiles\"
sName = Dir(sPath & "Data*.xls")
do while sName < ""
bFound = False
set bk = workbooks.open( sPath & sName)
for each sh in bk.worksheets
set sh1 = Nothing
On error resume Next
set sh1 = bk2.Worksheets(left(sh.name,6))
On Error goto 0
if not sh1 is nothing then
' copy from sh to sh1
bFound = True
else
' matching sheet not found
end if
Next sh
If not bFound then
msgbox "No match found in " & bk.name
end if
bk.close Savechanges:=False
sName = dir()
Loop
End Sub

--
Regards,
Tom Ogilvy


"Freddy" wrote:

By the way, the name of the Filter.xls file can vary depending on a
company
name. Can that be incorporated in your sample code? The latter portion
of the
file name will always have _Filter.xls. An example name is
"Company1_Filter.xls".

"Tom Ogilvy" wrote:

I check for the tab name he (I try to set a reference to the
sheet - if
unsuccessful, then I know the sheet doesn't exist).

for each sh in bk.worksheets
set sh1 = Nothing
On error resume Next
set sh1 = workbooks("Filter.xls").Worksheets(sh.name)
On Error goto 0
if not sh1 is nothing then
' copy from sh to sh1
bFound = True
else
' matching sheet not found
end if

if the Filter.xls actually has names of 6 characters, then you could
modify

set sh1 = workbooks("Filter.xls").Worksheets(sh.name)

to

set sh1 = workbooks("Filter.xls").Worksheets(left(sh.name,6) )

if you are looking for a match on the first 6 characters and both
names are
longer than six characters, then you would have to loop through the
names.

--
Regards,
Tom Ogilvy



"Freddy" wrote:

Tom, knowing that you based your sample code on the information I
supplied, a
new item has come up. I need to store the 6 leftmost characters of
the tab
name in the Data.xls files and use that as a basis to look in the
Filter.xls
file for a matching tab name. Also, I did not detect in your sample
code how
the tab names in the Filter.xls file were being evaluated. Please
review and
advise.

"Tom Ogilvy" wrote:

the following untested pseudo code should get you started:
Sub OpenFiles()
Dim sh as Worksheet, sh1 as Worksheet
dim bk as Workbook
Dim sName as String
Dim sPath as String
Dim bFound as Boolean
sPath = "C:\Myfiles\"
sName = Dir(sPath & "Data*.xls")
do while sName < ""
bFound = False
set bk = workbooks.open( sPath & sName)
for each sh in bk.worksheets
set sh1 = Nothing
On error resume Next
set sh1 = workbooks("Filter.xls").Worksheets(sh.name)
On Error goto 0
if not sh1 is nothing then
' copy from sh to sh1
bFound = True
else
' matching sheet not found
end if
Next sh
If not bFound then
msgbox "No match found in " & bk.name
end if
bk.close Savechanges:=False
sName = dir()
Loop
End Sub



--
Regards,
Tom Ogilvy


"Freddy" wrote:

I have a workbook (let's call it Filter.xls) containing several
tabs with
different names, let's say: 111111, 222222, and 333333. The
Filter.xls file
contains formulas which are applied to records of numerical
data obtained
from separate works (let's call them Data1.xls, Data2.xls,
Data3.xls, etc.)
containing several tabs with different names like 111111, xxxx,
222222, xxxx,
333333, xxxx. All aforementioned files would be in the same
working folder.

What I'd like the VBA code to do is to cycle through any
existing
Data[#].xls files and check if there are matching tab names
between them and
the Filter.xls file and, if so, copy the record set from the
matching tab in
the Data[#].xls file to the matching tab in the Filter.xls
file. I already
have code to copy the record set from the Data[#].xls file into
the
Filter.xls file but what I am missing is the code that
determines if there is
a match between the tabs in the Filter.xls file and any
available Data[#].xls
file. If there is no match then notify the user and exit.

I'd appreciate anyone's assistance. Please contact me if you
need further
details.