+ Reply to Thread
Results 1 to 10 of 10

Code not copying and transposing data from all other files in Directory

  1. #1
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Code not copying and transposing data from all other files in Directory

    User has a MASTER and two UPLOAD files in same Folder.

    Code should open each workbook in turn and, if is not the MASTER workbook, copy the data from Sheet 1 of the UPLOAD file, transpose and paste it to next free row on sheet 1 of MASTER, close the UPLOAD workbook, and move on to next one:

    Please Login or Register  to view this content.
    Any solutions, suggestions or alternatives welcome as ever.

    Ochimus
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this !


    Delete first Module1 then paste this VBA demonstration to the Sheet1 (COMBINED STOCK) worksheet module :

    PHP Code: 
    Sub Demo1()
        
    Dim P$, F$, R&
            
    ThisWorkbook.Path "\"
            F = Dir$(P & "
    *.xlsx"):  If F = "" Then Beep: Exit Sub
        With Application
            .DisplayAlerts = False
            .ScreenUpdating = False
        Do
                   R = UsedRange.Rows.Count - (UsedRange.Rows.Count > 1)
            With Workbooks.Open(P & F, 0).Sheets(1).UsedRange.Columns
                .Item(2 + (R = 1)).Resize(, .Count + (R > 1)).Copy
                 Cells(R, 1).PasteSpecial , , , True
                .Parent.Parent.Close False
            End With
                   F = Dir$
        Loop Until F = ""
            .DisplayAlerts = True
            .Goto [A1], True
            .ScreenUpdating = True
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 10-09-2021 at 07:01 PM. Reason: little optimization …

  3. #3
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Re: Code not copying and transposing data from all other files in Directory

    Marc,

    Appreciate the prompt response and suggestion.

    Unfortunately the line
    'F = Dir$(P & "*.xlsx"): If F = "" Then Beep: Exit Sub'
    throws a Run-Time error 52 ("Bad File Name or Number"), though can't see why as the MASTER and both UPLOADS on my machine are in the same Folder?

    And nor clear where the Code transposes the Cols and Rows?

    Ochimus

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Code not copying and transposing data from all other files in Directory


    No such issue on my side with your attachment, well works as expected so the bad is on your side …

    And PasteSpecial statement transposes the data.

  5. #5
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,026

    Re: Code not copying and transposing data from all other files in Directory

    Please Login or Register  to view this content.
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  6. #6
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Re: Code not copying and transposing data from all other files in Directory

    sintek,

    Appreciate your response and proposal, but sadly this also throws an error.

    I even replicated the Folder and Contents on the Hard Drive, in case the errors were, for whatever reason, because the files were in the Cloud.

    Attached screenshot demonstrates the Master and Upload files are all in the same Folder, and the two Code screenshots confirm the lines that generate the Error Messages which are also presented.

    Have no idea what "bad" Marc can suggest is causing the issue on the machine, so open to all suggestions?

    Ochimus
    Attached Images Attached Images

  7. #7
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,518

    Re: Code not copying and transposing data from all other files in Directory

    Just tested both codes and they work as expected.

    For Marc's code to work you need to put it in the Worksheet module of Combined stock. To do so right-click Combined Stock tab and select View Code.

    Paste his code in the right pane and run it from there.

    Sintek's code can be placed in a standard code module.
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  8. #8
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Code not copying and transposing data from all other files in Directory


    Ochimus,

    try to create a new folder on the local drive like C:\Tests\ then copy the 3 workbooks in this new folder
    then just follow the easy direction within my post or the previous post, it should work thus meaning it's a grant access issue …

  9. #9
    Valued Forum Contributor
    Join Date
    01-16-2012
    Location
    England
    MS-Off Ver
    MS 365 Version 2501 64-bit
    Posts
    1,491

    Re: Code not copying and transposing data from all other files in Directory

    Well, definitely interesting.

    Took Marc's suggestion, created Tests as a folder in the root drive.
    Marc's program, run from the worksheet, did the job but started on row 13 then when run again with a blank sheet, started from row 33, and so on. Then ran Sintek's Code from the Module, which ran perfectly.
    BUT when I created a "Tests" folder in the Cloud as a SUB Directory, both approaches fell over again!

    Never come across this issue before this, and no idea why it happens on this structure only?

    Ochimus

  10. #10
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2019 | 2021
    Posts
    15,026

    Re: Code not copying and transposing data from all other files in Directory

    Hard code your path...So change Thisworkbook.Path to your hard coded path...and test

    I assume your cloud path is something like..."https://"
    Last edited by Sintek; 10-11-2021 at 06:05 AM.

+ 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. VBA Code for Copying data from different files to Destination file
    By mtakawane in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-25-2018, 03:16 AM
  2. VBA code to open all files in a directory and move data to destination sheet
    By anthony777 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-08-2017, 04:45 PM
  3. [SOLVED] VBA code to open all files in a directory and move data to destination sheet
    By anthony777 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-07-2017, 04:10 PM
  4. Copying Files from One Directory to Another
    By tomtom412 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-16-2017, 04:55 PM
  5. VBA code to find .asc files in a directory
    By dubbdan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-11-2013, 05:41 PM
  6. Replies: 0
    Last Post: 03-13-2013, 09:08 PM
  7. Copying Files to Current Directory and Compiling an Outline
    By mike@lcs in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-02-2012, 10:00 AM

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