Results 1 to 11 of 11

Cycling through files in a folder

Threaded View

KateMolloy Cycling through files in a... 09-21-2009, 07:28 AM
sweep Re: Cycling through files in... 09-21-2009, 07:38 AM
KateMolloy Re: Cycling through files in... 09-21-2009, 08:33 AM
sweep Re: Cycling through files in... 09-21-2009, 09:08 AM
KateMolloy Re: Cycling through files in... 09-25-2009, 06:07 AM
sweep Re: Cycling through files in... 09-25-2009, 06:34 AM
KateMolloy Re: Cycling through files in... 09-25-2009, 11:38 AM
jaslake Re: Cycling through files in... 09-25-2009, 09:24 PM
broro183 Re: Cycling through files in... 09-26-2009, 06:47 PM
KateMolloy Re: Cycling through files in... 09-28-2009, 08:49 AM
broro183 Re: Cycling through files in... 09-28-2009, 04:18 PM
  1. #9
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Cycling through files in a folder

    hi Kate,

    This may need some fine tuning but hopefully it will do what is needed once the changes are made - my idea of not using copy & paste removes the need to flick between files, removes the risk of something else being mistakenly put on the clipboard or the clipboard being cleared while the macro is running (it may be faster too but I'm not sure). I've included the LastCell Function, rather than coding it into the same macro, as you may find it useful for other code development too.

    Kate/Dave, some of your code in posts 5 & 6 respectively seems to be missing the dot prefixes for the range or sheet within the With statements.

    - fingers crossed...
    Option Explicit
    
    Sub Test1()
    Dim strExtension As Variant 'change to what this is meant to be - I didn't know if it works as a string
    Dim strPath As String
    Dim wbOpen As Workbook
    Dim wbNew As Workbook
    Dim ConsolSht As Worksheet
    Dim FirstEmptyOnConsolSht As Range
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
        .EnableEvents = False
    End With
    
    Set wbNew = ThisWorkbook 'change as needed
    Set ConsolSht = wbNew.Worksheets("sheet1") 'change as needed
            Do While strExtension <> ""
                Set FirstEmptyOnConsolSht = ConsolSht.Range("a" & LastCell(ConsolSht).Row + 1).Resize(1, 2)
                Set wbOpen = Workbooks.Open(strPath & strExtension)
                With wbOpen
                    FirstEmptyOnConsolSht.Value = .Sheets("Sheet3").Range("A3:B3").Value
                    .Close SaveChanges:=False
                End With
                Set FirstEmptyOnConsolSht = Nothing
                strExtension = Dir
            Loop
    
    Set ConsolSht = Nothing
    Set wbNew = Nothing
    Set wbOpen = Nothing
    
    With Application
        .ScreenUpdating = true
        .DisplayAlerts = true
        .EnableEvents = true
    End With
    End Sub
    
    Function LastCell(ws As Worksheet) As Range
    ' sourced from http://www.beyondtechnology.com/geeks012.shtml
    'to identify the lastcell on a worksheet (& not necessarily the active sheet)
        Dim LastRow As Long
        Dim LastCol As Long
        ' Error-handling is here in case there is not any
        ' data in the worksheet
        On Error Resume Next
        With ws
            ' Find the last real row
            LastRow = .Cells.Find(What:="*", _
                                SearchDirection:=xlPrevious, _
                                SearchOrder:=xlByRows).Row
            LastRow = Application.WorksheetFunction.Max(1, LastRow)
            ' Find the last real column
            LastCol = .Cells.Find(What:="*", _
                                SearchDirection:=xlPrevious, _
                                SearchOrder:=xlByColumns).Column
            LastCol = Application.WorksheetFunction.Max(1, LastCol)
        End With
        On Error GoTo 0
        ' Finally, initialize a Range object variable for
        ' the last populated row.
        Set LastCell = ws.Cells(LastRow, LastCol)
    End Function
    hth
    Rob
    Last edited by broro183; 09-26-2009 at 06:50 PM. Reason: fixed code tags
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

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