Consulting

Results 1 to 11 of 11

Thread: Solved: Call for Data from folder

  1. #1
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location

    Solved: Call for Data from folder

    I have a spreadsheet that only has headers in Row 1. In the header i have a txtbox. I have some code to call up a calendar. I would like to know if when the calendar is called and the date is entered in the txtbox. I will have a button next to the txtbox labeled "GO". I would like for that button to grab the date and break it down so it know where to locate the files. So if the date is 12/22/2007. The script to pull this
    [vba]
    "C:\Packing Slip\" &
    Dec 2007 ("mm yyyy" & "\" &) 'This is the part it needs to grab from the date.
    Dec 22 ("mm dd" & "\" &) 'This is also a part it needs to grab from the date.
    [/vba]
    Then grab all .xls files in that folder. Then grab the data from column A2:O from all spreadsheets. How can i make this happen for all spreadsheet in that folder?

  2. #2
    VBAX Master XLGibbs's Avatar
    Joined
    Jan 2006
    Location
    state of confusion, but vacation in denial
    Posts
    1,315
    Location
    There are numerous methods depending on what you want to do when you "grab" them.

    Do a search of the excel forum for "Application.FileSearch" and "open all files in folder" to see samples you can work from.
    If you have posted the same question at multiple forums, please read this IMPORTANT INFO.

    Please use the thread tools to mark your thread Solved


    Please review the Knowledge Base
    for samples and solutions , or to submit your own!




  3. #3
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Don't use FileSearch, it is dropped in Excel 2007. Use Dir or FSO.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  4. #4
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    I searches several threads i can't seem to find anything im looking for. If anyone finds one please let me know.

    Thanks

  5. #5
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    I found this code

    [vba]Option Explicit
    Sub Example()
    Const strRootFolder_c As String = "C:\Depot Outgoing\"
    Const lngLwrBnd_c As Long = 1
    Const lngOffset_c As Long = 1
    Dim fs As Office.FileSearch
    Dim lngFileIndex As Long
    Dim wbNew As Excel.Workbook
    Dim wsTarget As Excel.Worksheet
    Dim wbCrnt As Excel.Workbook
    Dim wsOne As Excel.Worksheet
    Set fs = Excel.Application.FileSearch
    fs.NewSearch
    fs.FileType = msoFileTypeExcelWorkbooks
    fs.LookIn = strRootFolder_c & [f1] & "\" & [g1] & "\" & [h1]
    fs.Execute
    If fs.FoundFiles.Count < lngLwrBnd_c Then
    VBA.MsgBox _
    "Cannot find any workbooks in the specified root folder. Please check to make sure you have excel workbooks in the location specified. Operation aborted.", _
    vbExclamation Or vbSystemModal, "No Workbooks Found"
    Exit Sub
    End If
    Set wbNew = Excel.Workbooks.Add
    Set wsTarget = wbNew.Worksheets(lngLwrBnd_c)
    For lngFileIndex = lngLwrBnd_c To fs.FoundFiles.Count
    Set wbCrnt = Excel.Workbooks.Open(fs.FoundFiles(lngFileIndex), False, _
    False, Password:="foo")
    Set wsOne = wbCrnt.Worksheets(lngLwrBnd_c)
    wsOne.UsedRange.Copy wsTarget.Cells(wsTarget.UsedRange.Rows.Count + _
    lngOffset_c, lngLwrBnd_c)
    wbCrnt.Close False
    Next
    VBA.MsgBox "All worksheets have been merged.", vbInformation Or _
    vbSystemModal, "Operation Complete"
    End Sub[/vba]

    Im trying to see if i can make it work for me. But it's not working.
    I get the

    "Cannot find any workbooks in the specified root folder. Please check to make sure you have excel workbooks in the location specified. Operation aborted." Can someone see what's wrong.

  6. #6
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    Bump!

  7. #7
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    I have this code now but it's not doing anything for me. Can someone see what's missing.

    [vba]Sub CopyToMaster()
    Dim wbMaster As Workbook
    Dim wb As Workbook
    Dim strPath As String
    Dim strFile As String
    Dim lngRow As Long
    Dim i As String

    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    ' this assumes that the master workbook is active
    Set wbMaster = ActiveWorkbook

    StartDate = Format(Worksheets("Test").Range("c2").Value, "yyyy")
    MiddleDate = Format(Worksheets("Test").Range("c2").Value, "mmm yyyy")
    EndDate = Format(Worksheets("Test").Range("c2").Value, "mmm dd")


    strPath = "C:\Depot Outgoing\" & StartDate & "\" & MiddleDate & "\" & EndDate
    strFile = Dir(strPath & "*.xls", vbNormal)

    ' loop through all files in the folder
    Do Until strFile = ""
    ' if the master is in the same folder, make sure it's excluded
    If strFile <> "Master Pim.xls" Then
    ' find last row in column B
    lngRow = wbMaster.Test.Cells(Cells.Rows.Count, 2).End(xlUp).Row + 1

    Workbooks.Open strPath & strFile

    Set wb = ActiveWorkbook
    ' copy the data directly to the destination
    wb.Sheets(1).Range("a5:O3000").Copy wbMaster.Sheets(1).Range("B" & lngRow)

    wbMaster.Activate
    Test.Range("B" & lngRow + 2).PasteSpecial xlPasteValues

    Application.CutCopyMode = False
    wb.Close False
    End If
    ' find next file
    strFile = Dir()
    Loop

    ExitHere:
    Application.ScreenUpdating = True
    Exit Sub

    ErrHandler:
    MsgBox Err.Number & ": " & Err.Description
    Resume ExitHere
    End Sub[/vba]

  8. #8
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    This is the spreadsheet with a Test Tab.

  9. #9
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi Emoncada,

    Try this. See the comments in the code.

    [VBA]
    Sub CopyToMasterDR()

    Dim lngRow As Long
    Dim wb As Workbook
    Dim FileCount As Long 'Added for no files found check
    Dim strPath As String
    Dim strFile As String
    Dim wbMaster As Workbook
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    ' this assumes that the master workbook is active
    Set wbMaster = ActiveWorkbook

    'Make sure dir exists! Note hard coded Drive/Dir name and
    ' also VBA generated spaces in Month/Year and Month/Day eg:
    ' "C:\Depot Outgoing\2007\Dec 2007\Dec 27\"
    StartDate = Format(Worksheets("Test").Range("c2").Value, "yyyy")
    MiddleDate = Format(Worksheets("Test").Range("c2").Value, "mmm yyyy")
    EndDate = Format(Worksheets("Test").Range("c2").Value, "mmm dd")

    'Missing path separator in original code, Corrected with \
    strPath = "C:\Depot Outgoing\" & StartDate & "\" & MiddleDate & "\" & EndDate & "\"
    strFile = Dir(strPath & "*.xls", vbNormal)

    ' loop through all files in the folder
    Do Until strFile = ""
    ' if the master is in the same folder, make sure it's excluded
    'Added UCASE to do proper check
    If UCase(strFile) <> "MASTER PIM.XLS" Then

    'At least one file found. Mark as such.
    FileCount = 1

    ' find last row in column B
    lngRow = wbMaster.Sheets("Test").Cells(Cells.Rows.Count, 2).End(xlUp).Row + 1

    Workbooks.Open strPath & strFile

    Set wb = ActiveWorkbook
    ' copy the data diorectly to master sheet (sheet 1)
    wb.Sheets(1).Range("a5:O3000").Copy wbMaster.Sheets(1).Range("B" & lngRow)

    wbMaster.Activate
    'Recopy for PasteSpecial operation (kludge)
    wb.Sheets(1).Range("a5:O3000").Copy

    'Full command
    ActiveWorkbook.Sheets("Test").Range("B" & lngRow + 2).PasteSpecial _
    Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False

    Application.CutCopyMode = False
    wb.Close False
    End If
    ' find next file
    strFile = Dir()
    Loop

    ExitHere:
    'Check for no files found, inform user
    If FileCount < 1 Then
    MsgBox ("No files found in Directory " & strPath)
    End If
    Application.ScreenUpdating = True
    Exit Sub

    ErrHandler:
    MsgBox Err.Number & ": " & Err.Description
    Application.ScreenUpdating = True
    End Sub

    [/VBA]
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

  10. #10
    VBAX Expert
    Joined
    Apr 2007
    Location
    Orlando, FL
    Posts
    751
    Location
    That looks great rb Thanks So Much!

    Do you know if there is a way instead of saying a range.
    To have it detect the last row of data and then copy that range?

  11. #11
    VBAX Expert
    Joined
    Feb 2005
    Location
    Nanaimo, British Columbia, Cananda
    Posts
    568
    Location
    Hi,

    Detect the last row in which Column? In which sheet? Which Workbook?

    You currently have:

    wb.Sheets(1).Range("a5:O3000").Copy

    Is this the range you're speaking of?

    If so, is there a column in this range that will _always_ have data and therefore always contain the last row e.g. Col A will always have something in it even if Col B to O are blank.

    If so you can determine the last row using Column A (or any other Column of your choice.)

    See the comments in the revised code:

    [VBA]
    Sub CopyToMasterDR()
    Dim lngRow As Long
    Dim wb As Workbook
    Dim lastrow As Long '**Added for 'Range'
    Dim FileCount As Long 'Added for no files found check
    Dim strPath As String
    Dim strFile As String
    Dim wbMaster As Workbook
    On Error GoTo ErrHandler
    Application.ScreenUpdating = False

    ' this assumes that the master workbook is active
    Set wbMaster = ActiveWorkbook
    'Make sure dir exists! Note hard coded Drive/Dir name and
    ' also VBA generated spaces in Month/Year and Month/Day eg:
    ' "C:\Depot Outgoing\2007\Dec 2007\Dec 27\"
    StartDate = Format(Worksheets("Test").Range("c2").Value, "yyyy")
    MiddleDate = Format(Worksheets("Test").Range("c2").Value, "mmm yyyy")
    EndDate = Format(Worksheets("Test").Range("c2").Value, "mmm dd")
    'Missing path separator in original code, Corrected with \
    strPath = "C:\Depot Outgoing\" & StartDate & "\" & MiddleDate & "\" & EndDate & "\"
    strFile = Dir(strPath & "*.xls", vbNormal)
    ' loop through all files in the folder
    Do Until strFile = ""
    ' if the master is in the same folder, make sure it's excluded
    'Added UCASE to do proper check
    If UCase(strFile) <> "MASTER PIM.XLS" Then
    'At least one file found. Mark as such.
    FileCount = 1
    ' find last row in column B in MASTER SHEET
    lngRow = wbMaster.Sheets("Test").Cells(Cells.Rows.Count, 2).End(xlUp).Row + 1
    Workbooks.Open strPath & strFile
    Set wb = ActiveWorkbook
    '** Find last row of data in Wb to copy FROM
    lastrow = Range("A65536").End(xlUp).Row
    ' copy the data directly to master sheet (sheet 1)
    'USING LASTROW
    wb.Sheets(1).Range("a5:O" & lastrow).Copy wbMaster.Sheets(1).Range("B" & lngRow)
    wbMaster.Activate
    'Recopy for PasteSpecial operation (kludge)
    'USING LASTROW
    wb.Sheets(1).Range("a5:O" & lastrow).Copy
    'Full command
    ActiveWorkbook.Sheets("Test").Range("B" & lngRow).PasteSpecial _
    Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
    False, Transpose:=False
    Application.CutCopyMode = False
    wb.Close False
    End If
    ' find next file
    strFile = Dir()
    Loop
    ExitHere:
    'Check for no files found, inform user
    If FileCount < 1 Then
    MsgBox ("No files found in Directory " & strPath)
    End If
    Application.ScreenUpdating = True
    Exit Sub
    ErrHandler:
    MsgBox Err.Number & ": " & Err.Description
    Application.ScreenUpdating = True
    End Sub
    [/VBA]
    Last edited by rbrhodes; 12-27-2007 at 12:21 AM.
    Cheers,

    dr

    "Questions, help and advice for free, small projects by donation. large projects by quote"

    http:\\www.ExcelVBA.joellerabu.com

Posting Permissions

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