+ Reply to Thread
Results 1 to 19 of 19

copy rows from 42 workbooks into masterworkbook

Hybrid View

kandre copy rows from 42 workbooks... 10-03-2012, 11:32 AM
kandre Re: copy rows from 42... 10-03-2012, 11:45 AM
kandre Re: copy rows from 42... 10-03-2012, 12:47 PM
kandre Re: copy rows from 42... 10-03-2012, 02:44 PM
JBeaucaire Re: copy rows from 42... 10-03-2012, 03:02 PM
kandre Re: copy rows from 42... 10-03-2012, 04:00 PM
Winon Re: copy rows from 42... 10-03-2012, 03:51 PM
JBeaucaire Re: copy rows from 42... 10-03-2012, 04:12 PM
kandre Re: copy rows from 42... 10-03-2012, 04:15 PM
JBeaucaire Re: copy rows from 42... 10-03-2012, 04:21 PM
kandre Re: copy rows from 42... 10-03-2012, 04:37 PM
JBeaucaire Re: copy rows from 42... 10-03-2012, 06:31 PM
kandre Re: copy rows from 42... 10-04-2012, 01:41 AM
JBeaucaire Re: copy rows from 42... 10-04-2012, 02:28 AM
kandre Re: copy rows from 42... 10-04-2012, 02:36 AM
JBeaucaire Re: copy rows from 42... 10-04-2012, 02:37 AM
kandre Re: copy rows from 42... 10-04-2012, 05:26 AM
kandre Re: copy rows from 42... 10-04-2012, 05:58 AM
JBeaucaire Re: copy rows from 42... 10-04-2012, 10:08 AM
  1. #1
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    copy rows from 42 workbooks into masterworkbook

    Hey

    I,m looking for a more elegant solution to the macro pasted below. The idea is to copy 6 rows from 42 workbooks in one single workbook without having to copy the below macro 42 times. All files will be in one folder. So the steps will be:

    1 open first workbook
    2 mark rows
    3 copy rows
    4 insert into active workbook
    5 close workbook
    6 repeat 42 times with every file in directory

        Workbooks.Open Filename:= _
            "\\c\folder\filename 1.xlsm"
        Range("13:15,30:32").Select
        Range("A30").Activate
        Selection.EntireRow.Hidden = False
        Selection.Copy
        Windows("Book1").Activate
        Rows("3:3").Select
        ActiveSheet.Paste
        Windows("filename 1.xlsm").Activate
        Range("B4").Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Book1").Activate
        Range("A3:A8").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("filename 1.xlsm").Activate
        ActiveWindow.Close
        
        Workbooks.Open Filename:= _
            "\\c\foldername\filename 2.xlsm"
        Range("13:15,30:32").Select
        Range("A30").Activate
        Selection.EntireRow.Hidden = False
        Selection.Copy
        Windows("Book1").Activate
        Rows("9:9").Select
        ActiveSheet.Paste
        Windows("Trafikkinntektsmodell B2013 AT.xlsm").Activate
        Range("B4").Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows("Book1").Activate
        Range("A9:A14").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
            :=False, Transpose:=False
        Windows("filename 2.xlsm").Activate
        ActiveWindow.Close
    Last edited by JBeaucaire; 10-03-2012 at 02:53 PM. Reason: Added code tags, as per forum rules. Don't forget!

  2. #2
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    The command must copy the 6 rows under eachother, such that copyfunction will start copy from rows from first work book to row 2-6 in masterworkbook then 7 to 12 in next loop and so on.

  3. #3
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    So my question is how do i write a single macro that copies the 6 rows i need into a second workbook without overlapping eachother

  4. #4
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    can anyone help please?

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    Something like this should work:
    Option Explicit
    
    Sub ImportData()
    'This macro is installed into the master workbook
    
    Dim fPATH As String, fNAME As String, NR As Long
    Dim wbDATA As Workbook, wsMASTER As Worksheet
    
    Set wsMASTER = ThisWorkbook.Sheets("Sheet1")                'edit this to the name of the master sheet
    NR = wsMASTER.Range("A" & Rows.Count).End(xlUp).Row + 1     'next empty row on the master sheet
    
    fPATH = "\\c\folder\"                                       'path to all files
    fNAME = Dir(fPATH & "*.xls*")                               'get the first filename from the folder
    
    Do While Len(fNAME) > 0                                     'process one file at a time til no more filenames
        Set wbDATA = Workbooks.Open(fPATH * fNAME)              'open the found file
        With Range("13:15,30:32")
            .EntireRow.Hidden = False                           'unhide the rows
            .Copy wsMASTER.Range("A" & NR)                      'copy into master sheet to empty rows
        End With
        wsMASTER.Range("A" & NR).Resize(6).Value = Range("B4").Value    'add key value in column A
    
        wbDATA.Close False                                      'close the found file
        NR = wsMASTER.Range("A" & Rows.Count).End(xlUp).Row + 1 'next empty row on the master sheet
        fNAME = Dir                                             'get the next filename
    Loop
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    Hmmm. no luck. what if i upload master workbook and one of the dataimport workbooks?
    Last edited by JBeaucaire; 10-03-2012 at 04:18 PM. Reason: Removed whole post quote, unnecessary.

  7. #7
    Forum Guru Winon's Avatar
    Join Date
    02-20-2007
    Location
    East Rand, R.S.A.
    MS-Off Ver
    2010
    Posts
    6,113

    Re: copy rows from 42 workbooks into masterworkbook

    @ JBeaucaire,

    All you Moderators are much to clever for me, But hang in there, take a break, give me 100 years PLEASE, and then, don't even try to take me to task!

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    Describe what "no luck" means.

  9. #9
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    no execution of makro. Just to clarify under statement

                    fNAME = Dir(fPATH & "*.xls*")                               'get the first filename from the folder
    I replace everything inside the colon with whole filename? the first filename in the folder is "trafikkinntektsmodell B2013 AL.xlsm". Every file after that will have same name except the two letter abbreviation
    Last edited by JBeaucaire; 10-03-2012 at 04:18 PM. Reason: Added code tags, as per forum rules. Don't forget!

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    That line of code should work just fine. The * wildcards in the filename should allow any names. You could be a little more specific, but it's not really necessary:

                    fNAME = Dir(fPATH & "trafikkinntektsmodell*.xls*")                               'get the first filename from the folder

    I would recommend using F8 to step through that macro one line of code at a time and see which line of code does NOT do what it is supposed to. After you pass the Fname = code, hover your mouse over fNAME and you should see the name of the file it found. Anything?

  11. #11
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    yeah. It hit run time error 52 Bad file name or error when it hit fNAME

    I retraced the macro code and when i hover over fNAME.. it shows fNAME=""

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    So, VBA is not finding any files in that folder. You did edit the folder highlighted in red in the original macro to point to your actual folder?

    I don't do any "over network" work, so if the problem is related to running the VBA over a network path I won't be much help with that. Only tip I can think of would be to map the network drive to your own system as new drive so you can use standard Windows path formatting:

    fPATH = "C:\Path\To\My\Files\"

  13. #13
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    Hey, yes. I did edit the folder path like this

    fPATH = "C:\trafikk\"

    Quote Originally Posted by JBeaucaire View Post
    So, VBA is not finding any files in that folder. You did edit the folder highlighted in red in the original macro to point to your actual folder?

    I don't do any "over network" work, so if the problem is related to running the VBA over a network path I won't be much help with that. Only tip I can think of would be to map the network drive to your own system as new drive so you can use standard Windows path formatting:

    fPATH = "C:\Path\To\My\Files\"

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    ok, well, I use this kind of code in 100s of macros. Something doesn't match what is actually in your working environment.

  15. #15
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    Strange. If could use the rest of the code after this then... would that work? And copy 42 times with but just changing filename?

     Sub ImportData()
    'This macro is installed into the master workbook
    
        Dim NR As Long
        Dim wbDATA As Workbook, wsMASTER As Worksheet
    
        Set wsMASTER = ThisWorkbook.Sheets("calc")                'edit this to the name of the master sheet
        NR = wsMASTER.Range("A" & Rows.Count).End(xlUp).Row + 1     'next empty row on the master sheet
    
        Set wbDATA = Workbooks.Open("\\sgm434\avdelinger2$\CA\RL\Stab RL\Controller\2013\Budsjett\Trafikk\Samtlige\trafikkinntektsmodell B2013 AL.xlsm")              'open the found file
        With Range("13:15,30:32")
            .EntireRow.Hidden = False                           'unhide the rows
            .Copy wsMASTER.Range("A" & NR)                      'copy into master sheet to empty rows
        End With
        wsMASTER.Range("A" & NR).Resize(6).Value = Range("B4").Value    'add key value in column A
    
        wbDATA.Close False                                      'close the found file
        NR = wsMASTER.Range("A" & Rows.Count).End(xlUp).Row + 1 'next empty row on the master sheet
    Last edited by kandre; 10-04-2012 at 05:25 AM.

  16. #16
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    Ok, I've added code tags to TWO of your prior posts. It's your turn. Please correct the post above as per forum rules.

    EDIT post #15, highlight the code, click the # icon.

    Take a moment and read through the forum rules, you are expected to know and follow them. Thanks.

    Ping back when you have corrected it.
    Last edited by JBeaucaire; 10-04-2012 at 03:57 AM.

  17. #17
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    Sorry bout that. What line of programming do i need to define if the file isnt there then skip to next file?

  18. #18
    Registered User
    Join Date
    10-03-2012
    Location
    oslo
    MS-Off Ver
    Excel 2010
    Posts
    16

    Re: copy rows from 42 workbooks into masterworkbook

    btw, the code now posted in post 15 worked like a charm

  19. #19
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: copy rows from 42 workbooks into masterworkbook

    I have marked this thread solved for you.
    In the future please select Thread Tools from the menu above and mark the thread as solved. Thanks.

+ Reply to Thread

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