+ Reply to Thread
Results 1 to 3 of 3

open the workbook in a folder

Hybrid View

  1. #1
    Registered User
    Join Date
    04-23-2008
    Posts
    48

    open the workbook in a folder

    Dear all,,

    Good day,
    I am facing to run a macro in Office 2007 but it is working fine in Office 2003. Below is my coding:
    i need to browse to the folder and open 1 by 1 workbook in the the folder: however, it seems this coding cannot run in office 2007. please help.


    Dim lCount As Long
        Dim wbResults As Workbook
        Dim wbThis As Workbook
        Dim dblValue As Double
        Dim WbCnt  As Long
    
        With Application
            .ScreenUpdating = False
            .DisplayAlerts = False
            .EnableEvents = False
    
            On Error Resume Next
    
            Set wbThis = ThisWorkbook
            dblValue = 0
            WbCnt = 0
            With .FileSearch
                .NewSearch
                .LookIn = "C:\Service_IPL\Generated" '<-Change path to suit
                .FileType = msoFileTypeExcelWorkbooks
    
                If .Execute > 0 Then    'Workbooks in folder
                    For lCount = 1 To .FoundFiles.Count    'Loop through all.
                        'Open Workbook x and Set a Workbook variable to it
                        
                          
                        Set wbResults = Workbooks.Open(filename:=.FoundFiles(lCount), UpdateLinks:=0)
                      'your code here
                                        
                    With ThisWorkbook.Sheets(1)
                                    
                        WbCnt = WbCnt + 1
                    Next lCount
              End If
            End With
    
           
            On Error GoTo 0
            .ScreenUpdating = True
            .DisplayAlerts = True
            .EnableEvents = True
        End With

  2. #2
    Forum Contributor johnjohns's Avatar
    Join Date
    11-19-2007
    Location
    Dubai, UAE
    MS-Off Ver
    2003 and 2007
    Posts
    526

    Re: open the workbook in a folder

     Application.FileSearch
    does not work in excel 2007. See the answers I got for a similar query of mine
    https://www.excelforum.com/showthread.php?t=711455

    rgds

    johnjohns

  3. #3
    Valued Forum Contributor mohan.r1980's Avatar
    Join Date
    09-18-2010
    Location
    Mumbai, India
    MS-Off Ver
    Excel 2010 (windows7)
    Posts
    729

    Re: open the workbook in a folder

    Hi Friend,

    As per your title that "open the workbook in a folder",
    Try below code
    You may change as per your requirement
    Private Sub openFile()
    
        Dim fPath As String:    fPath = "C:\Documents and Settings\Desktop\Target\"
        Dim fName As String
        Dim Cnt As Long
    
        fName = Dir(fPath & "*.xls")
    
        If Len(fName) = 0 Then
        MsgBox "Please Paste the Path, where you want to save the files!!!!!!"
        Exit Sub
        Else
        Do While Len(fName) > 0
            Cnt = Cnt + 1
        Workbooks.Open Filename:=fName
    
            fName = Dir
    
         Loop
    End If
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1