Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / March 2006

Tip: Looking for answers? Try searching our database.

searching for a worksheet name while going through a subdirectory

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ibbm - 23 Mar 2006 01:11 GMT
I have a master spreadsheet in which I search through a subdirectory to
update it with monthly data.
This works fine until I try to do an if statement that  checks for the name
of the worksheet in the workbook it is currently reading..  Here is the code
below.  I thank you in advance for your help.  When I run this I get **WRONG
for all of them.

   Application.ScreenUpdating = False
   Set FS = Application.FileSearch
   strPath = "x:\By Store Number"
   szSummary = ActiveWorkbook.Name
   strMonth = "Feb"
   strMonth1 = "Canada February"
   Sheets(1).Name = strMonth1
   strMonth2 = "USA February"
   Sheets(2).Name = strMonth2
   strOther = "Country not Defined"
   Sheets(3).Name = strOther
   
   
   n = 1
   
   With FS
       .NewSearch
       .LookIn = strPath
       .SearchSubFolders = True
       '.FileType = msoFileTypeExcelWorkbooks
       .Filename = strMonth
       iCount = .Execute
       strMessage = Format(iCount, "0 ""Files Found""")
       
       For Each vaFileName In .FoundFiles
             'MsgBox strMessage
           'strMessage = strMessage & vbCr & vaFileName
             Set wb = Workbooks.Open(vaFileName)
                           
             ' get info of this workbook to update the summary
             ' sales figures
             
             Workbooks(szSummary).Activate
             'If worksheet has an information sheet then it's valid else
flag it
             For Each ws In wb.Worksheets
               If ws.Name = "Information" Then
                   ThisWorkbook.Sheets(1).Cells(n, 1) =
wb.Sheets("Information").Cells(5, 2)
                   ThisWorkbook.Sheets(1).Cells(n, 2) =
wb.Sheets("Information").Cells(4, 2)
               Else
                   ThisWorkbook.Sheets(1).Cells(n, 1) = "**WRONG"
                   ThisWorkbook.Sheets(1).Cells(n, 2) =
wb.Sheets(2).Cells(3, 2)
               End If
               Next ws
             ThisWorkbook.Sheets(1).Cells(n, 3) = wb.Sheets("Royalty
Report").Cells(13, 2)
             ThisWorkbook.Sheets(1).Cells(n, 4) = wb.Sheets("Royalty
Report").Cells(13, 3)
Jim Cone - 23 Mar 2006 06:02 GMT
       For Each vaFileName In .FoundFiles
             Set wb = Workbooks.Open(vaFileName)
             Workbooks(szSummary).Activate
             ThisWorkbook.Sheets(1).Cells(n, 1) = "**WRONG"
             ThisWorkbook.Sheets(1).Cells(n, 2) = wb.Sheets(2).Cells(3, 2)
             For Each ws In wb.Worksheets
                 If ws.Name = "Information" Then
                    ThisWorkbook.Sheets(1).Cells(n, 1) = wb.Sheets("Information").Cells(5, 2)
                    ThisWorkbook.Sheets(1).Cells(n, 2) = wb.Sheets("Information").Cells(4, 2)
                    Exit For
                 End If
             Next ws
             ThisWorkbook.Sheets(1).Cells(n, 3) = wb.Sheets("Royalty Report ").Cells(13, 2)
             ThisWorkbook.Sheets(1).Cells(n, 4) = wb.Sheets("Royalty Report ").Cells(13, 3)
       Next
Signature

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

"ibbm" <ibbm@discussions.microsoft.com>
wrote in message...
I have a master spreadsheet in which I search through a subdirectory to
update it with monthly data.
This works fine until I try to do an if statement that  checks for the name
of the worksheet in the workbook it is currently reading..  Here is the code
below.  I thank you in advance for your help.  When I run this I get **WRONG
for all of them.

   Application.ScreenUpdating = False
   Set FS = Application.FileSearch
   strPath = "x:\By Store Number"
   szSummary = ActiveWorkbook.Name
   strMonth = "Feb"
   strMonth1 = "Canada February"
   Sheets(1).Name = strMonth1
   strMonth2 = "USA February"
   Sheets(2).Name = strMonth2
   strOther = "Country not Defined"
   Sheets(3).Name = strOther    
   n = 1
   
   With FS
       .NewSearch
       .LookIn = strPath
       .SearchSubFolders = True
       '.FileType = msoFileTypeExcelWorkbooks
       .Filename = strMonth
       iCount = .Execute
       strMessage = Format(iCount, "0 ""Files Found""")
       
       For Each vaFileName In .FoundFiles
             'MsgBox strMessage
           'strMessage = strMessage & vbCr & vaFileName
             Set wb = Workbooks.Open(vaFileName)                            
             ' get info of this workbook to update the summary sales figures              
             Workbooks(szSummary).Activate
             'If worksheet has an information sheet then it's valid else flag it
             For Each ws In wb.Worksheets
               If ws.Name = "Information" Then
                   ThisWorkbook.Sheets(1).Cells(n, 1) = wb.Sheets("Information").Cells(5, 2)
                   ThisWorkbook.Sheets(1).Cells(n, 2) = wb.Sheets("Information").Cells(4, 2)
               Else
                   ThisWorkbook.Sheets(1).Cells(n, 1) = "**WRONG"
                   ThisWorkbook.Sheets(1).Cells(n, 2) = wb.Sheets(2).Cells(3, 2)
               End If
               Next ws
             ThisWorkbook.Sheets(1).Cells(n, 3) = wb.Sheets("Royalty Report").Cells(13, 2)
             ThisWorkbook.Sheets(1).Cells(n, 4) = wb.Sheets("Royalty Report").Cells(13, 3)

ibbm - 27 Mar 2006 21:12 GMT
This did the trick. Awesome thanks so much!

>         For Each vaFileName In .FoundFiles
>               Set wb = Workbooks.Open(vaFileName)
[quoted text clipped - 11 lines]
>               ThisWorkbook.Sheets(1).Cells(n, 4) = wb.Sheets("Royalty Report ").Cells(13, 3)
>         Next
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.