Consulting

Results 1 to 11 of 11

Thread: Solved: FileSearch VBA

  1. #1
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location

    Solved: FileSearch VBA

    Does anyone know how to modify the following?! I've only just discovered that the FileSearch function is not available in Excel 2007...

     
    Sub ImportData()
        Dim fs As FileSearch
        Dim NewWB As Workbook
        Dim wks As Worksheet
        Dim msg As String, _
        orgWs As String
        msg = "Warning: Import External Data?" & vbNewLine & _
        vbNewLine & "Select OK to continue, or Cancel to exit"
        If MsgBox(msg, vbOKCancel) = vbCancel Then Exit Sub
        
        Application.ScreenUpdating = False
        Set fs = Application.FileSearch
        With fs
        .LookIn = "C:\Documents and Settings\DAVES\Desktop\TEST"
        .FileType = msoFileTypeExcelWorkbooks
        End With
        With fs
        If .Execute() > 0 Then
        For i = 1 To .FoundFiles.Count
        If Not .FoundFiles(i) Like "*Main.xls" Then
        Set NewWB = Workbooks.Open(.FoundFiles(i))
        NewWB.Sheets(1).Range(NewWB.Sheets(1).Range("A2"), NewWB.Sheets(1).Range("Q65536").End(xlUp)).Copy
        ThisWorkbook.Sheets(1).Range("C65536").End(xlUp).Offset(1).PasteSpecial xlValues
        Application.CutCopyMode = False
        NewWB.Close False
        End If
                     
        Next
        End If
        End With
    End Sub
    Any help would be much appreciated.

    Thanks

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Give this a try
    [vba]
    Option Explicit
    Sub ImportData()
    Dim Pth As String
    Dim MyFile As String
    Dim msg As String

    msg = "Warning: Import External Data?" & vbNewLine & _
    vbNewLine & "Select OK to continue, or Cancel to exit"
    If MsgBox(msg, vbOKCancel) = vbCancel Then Exit Sub
    Application.ScreenUpdating = False
    Pth = "C:\Documents and Settings\DAVES\Desktop\TEST\"
    MyFile = Dir(Pth & "*.xls")
    Do Until MyFile = ""
    Call DoStuff(Pth, MyFile)
    MyFile = Dir
    Loop
    End Sub

    Sub DoStuff(Pth As String, MyFile As String)
    Dim NewWB As Workbook
    If Not MyFile Like "*Main.xls" Then
    Set NewWB = Workbooks.Open(Pth & MyFile)
    NewWB.Sheets(1).Range(NewWB.Sheets(1).Range("A2"), NewWB.Sheets(1).Range("Q65536").End(xlUp)).Copy
    ThisWorkbook.Sheets(1).Range("C65536").End(xlUp).Offset(1).PasteSpecial xlValues
    Application.CutCopyMode = False
    NewWB.Close False
    End If
    End Sub
    [/vba]
    Last edited by mdmackillop; 04-14-2010 at 10:48 AM. Reason: Errant "!" removed.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Hi mdmackillop,

    Thanks for all your help thus far.

    I've tried the code you've so kindly supplied but it doesn't seem to be working.

    What I am trying to do is open all the files in a given folder, and copy the entire contents from a given sheet, in each workbook, say sheet1 for arguments sake, and paste the values into sheet "ABC" in my active workbook.

    The code that I had originally supplied did do the trick - prior to Excel 2007 being installed on my machine.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Hi Dave,

    I think just an errant keystroke. Try removing the exclamation point at the end of 'Pth'.

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Well spotted Mark!!!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Shucks Malcom, I could not seem to spot Set Rng() in the other to save my scalp... (and then I was still not following well... )

    A great day to you, off to bed for this lad.

  7. #7
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Thanks guys....

    The code seems to be partly working.

    A runtime error (ActiveX component cannot create object) is received when the code hits:

     
    ThisWorkbook.Sheets("MasterInbound").Range("C65536").End(xlUp).Offset(1).PasteSpecial xlValues
    Any thoughts?!

    Even when I replace MasterInbound with a sheet number the same error seems to occur?!

  8. #8
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I would think you would get a 'subscript out of range' error, if there was a problem on that line. Can you post the code, or a sample wb w/the code as you currently have it written?

  9. #9
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    Hi GTO,

    What I am trying to do is open all the files in a given folder, and copy the entire contents from a given sheet, in each workbook, say "ABC" for arguments sake, and paste the values into sheet "DEF" in my active workbook.

    the code is as follows:

     
    Sub ImportInbound()
        Dim Pth As String
        Dim MyFile As String
        Dim msg As String
         
        msg = "Warning: Import External Data?" & vbNewLine & _
        vbNewLine & "Select OK to continue, or Cancel to exit"
        If MsgBox(msg, vbOKCancel) = vbCancel Then Exit Sub
        Application.ScreenUpdating = False
        Pth = "C:\Documents and Settings\DAVES\Desktop\TEST\"
        MyFile = Dir(Pth & "*.xls")
        Do Until MyFile = ""
            Call AppendInbound(Pth, MyFile)
            MyFile = Dir
        Loop
    End Sub
    Sub AppendInbound(Pth As String, MyFile As String)
        Dim NewWB As Workbook
        If Not MyFile Like "*Main.xls" Then
            Set NewWB = Workbooks.Open(Pth & MyFile)
            NewWB.Sheets(1).Range(NewWB.Sheets(1).Range("C11"), NewWB.Sheets(1).Range("S65536").End(xlUp)).Copy
            ThisWorkbook.Sheets(1).Range("C65536").End(xlUp).Offset(1).PasteSpecial xlValues
            Application.CutCopyMode = False
            NewWB.Close False
        End If
    End Sub
    It seems to open the files in say Folder1, copying the contents of "ABC", however it falls when it hits:

    ThisWorkbook.Sheets(1).Range("C65536").End(xlUp).Offset(1).PasteSpecial xlValues
    Regardless, of whether or not I specify the sheet name of sheet number, the same problem occurs.

  10. #10
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    I am afraid I cannot replicate the failure, sorry.

    Mark

  11. #11
    VBAX Regular
    Joined
    Aug 2009
    Posts
    44
    Location
    I've managed to solve the problem by tweaking the code:

     
    Sub ImportInbound()
        Dim Pth As String
        Dim MyFile As String
        Dim msg As String
         
        msg = "Warning: Import External Data?" & vbNewLine & _
        vbNewLine & "Select OK to continue, or Cancel to exit"
        If MsgBox(msg, vbOKCancel) = vbCancel Then Exit Sub
        Application.ScreenUpdating = False
        Pth = "C:\Documents and Settings\DAVES\Desktop\TEST\"
        MyFile = Dir(Pth & "*.xls")
        Do Until MyFile = ""
            Call AppendInbound(Pth, MyFile)
            MyFile = Dir
        Loop
    End Sub
    Sub AppendInbound(Pth As String, MyFile As String)
        Dim NewWB As Workbook
        Dim wks As Worksheet
        If Not MyFile Like "*Main.xls" Then
            Set NewWB = Workbooks.Open(Pth & MyFile)
            NewWB.Sheets(1).Range(NewWB.Sheets(1).Range("C11"), NewWB.Sheets(1).Range("Q65536").End(xlUp)).Copy
            Windows("DI Master TEST.xlsm").Activate
            Sheets("MasterInbound").Range("C65536").End(xlUp).Offset(1).PasteSpecial xlValues
            Application.CutCopyMode = False
            NewWB.Close False
        End If
    End Sub

Posting Permissions

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