+ Reply to Thread
Results 1 to 4 of 4

Macro does not work

Hybrid View

  1. #1
    Registered User
    Join Date
    04-11-2020
    Location
    Montreal
    MS-Off Ver
    10
    Posts
    4

    Macro does not work

    Hello,

    I need to copy all the files in the same folder and paste to a combined file.
    However, it's always the case that the first files in the folder are not copied fully.
    For example, in a folder with 50 files.
    File 1 has 2000 lines, and only 500 lines are copied.
    File 2 has 3000 lines, and only 300 lines are copied.
    ...
    File 7 has 1000 lines, and only 10 lines are copied.

    The rest, from file 8- 50 are okie (although some files not 100% of the lines are copied).

    Could you help me to understand why the below code always have problem for the first several files in the folder?

    Really appreciate your help!
    Chau

    =======================================================================================

    Sub simpleXlsMerger()
        
        Dim bookList As Workbook
        
        
        
        Dim mergeObj As Object, dirObj As Object, filesObj As Object, everyObj As Object
        Application.ScreenUpdating = False
        Set mergeObj = CreateObject("Scripting.FileSystemObject")
        Set dirObj = mergeObj.Getfolder("O:\Entrepot\Group\Whse\CHAU\Combine files into 1 sheet\PUROLATOR")
        Set filesObj = dirObj.Files
        For Each everyObj In filesObj
        Set bookList = Workbooks.Open(everyObj)
        
        Dim sht As Worksheet
        Set sht = ThisWorkbook.Worksheets("Combined")
        
        Dim LastRow As Long
        LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
        Range("A2:GF" & LastRow).Copy
        ThisWorkbook.Worksheets(1).Activate
        Range("A500000").End(xlUp).Offset(1, 0).PasteSpecial
        Application.CutCopyMode = False
        bookList.Close
        Next
        
    End Sub
    =================================================
    Last edited by FDibbins; 01-28-2021 at 01:26 AM.

  2. #2
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    52,946

    Re: Macro does not work

    Administrative Note:

    Welcome to the forum.

    We would very much like to help you with your query, however you need to include code tags around your code.

    Please take a moment to add the tags. Posting code between tags makes your code much easier to read and copy for testing, and it also maintains VBA formatting.

    Please see Forum Rule #2 about code tags and adjust accordingly. Click on Edit to open your post, then highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here

    (Note: I have added them for you - this time
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  3. #3
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,548

    Re: Macro does not work

    Try changing this
    LastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
    to this
    LastRow = sht.Cells.Find("*", ,xlValues , , xlByRows, xlPrevious).Row
    and this
    ThisWorkbook.Worksheets(1).Activate
    Range("A500000").End(xlUp).Offset(1, 0).PasteSpecial
    to this
    ThisWorkbook.Worksheets(1).Activate
    Cells.Find("*", ,xlValues , , xlByRows, xlPrevious).Offset(1, 0).PasteSpecial

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Macro does not work

    Sub simpleXlsMerger()
        
        Dim objFolder As Object, objFile As Object
        Dim LastRow As Long
        
        Application.ScreenUpdating = False
        
        Set objFolder = CreateObject("Scripting.FileSystemObject").Getfolder("O:\Entrepot\Group\Whse\CHAU\Combine files into 1 sheet\PUROLATOR")
        
        For Each objFile In objFolder.Files
            With Workbooks.Open(objFile)
                With .Worksheets("Combined")
                    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
                    .Range("A2:GF" & LastRow).Copy
                    ThisWorkbook.Worksheets(1).Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial
                    Application.CutCopyMode = False
                End With
                .Close SaveChanges:=False
            End With
        Next
        
        Application.ScreenUpdating = True
        
    End Sub
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Need someone to edit Modify Macro's to work on entire work book
    By MarkKil in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-24-2018, 01:01 AM
  2. [SOLVED] Macro for auto work allocation depending on old pending work?
    By nr6281 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 09-27-2017, 04:07 PM
  3. [SOLVED] Macro work in personal workbook, doesn't work in other workbooks
    By Centexcel in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-30-2013, 11:47 AM
  4. [SOLVED] Macro Doesn't Work Through Button, Does Work Through Developer ->Macros Option
    By freybe06 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-27-2013, 11:55 AM
  5. [SOLVED] How to make the macro work for all rows in the work sheet
    By Valli nayaki in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-18-2013, 10:43 PM
  6. Macro to collate data from different work books and different work sheets
    By bvdileep in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-04-2012, 05:34 AM
  7. Replies: 2
    Last Post: 07-11-2006, 11:15 PM

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